How to store a single selected row value in variable? - php

entry in row "cars" is "BMWx1"
$sql = "SELECT cars FROM table LIMIT 1";
$result = $con->query($sql);
if ($result->num_rows > 0) {
$rows[] = $result->fetch_row();
$result->free();
}
echo json_encode($rows[0]);
output: ["BMWx1"]
So far so good. Now how do I get my variable $cars to be equal to "BMWx1"
e.g.:
echo $cars;
output: BMWx1
Thanks you

If you're just reading a single row, don't push the row onto an array. fetch_assoc returns an associative array containing the row that was retrieved from the query (or false if there was nothing selected). Just use that and access the column you want.
$result = $con->query($sql);
$row = $result->fetch_assoc();
if ($row) {
$cars = $row['cars'];
}

Related

How to get all rows from myslq table by foreach loop in php pdo

I am trying to collect all rows from a table of mySql database. But I am getting 1 row lesser than the original rows. Suppose the table has 3 rows but I am getting data of 2 rows. I am missing the first one always. Here is the image of my table.
Here is my code:
$query = $db->query("SELECT * FROM `POS_refund`");
if($query->fetchColumn() > 0){
foreach($query as $row){
echo $get_prod_id = $row['prod_id'];
$get_prod_qnt = $row['qnt'];
}
}
Where is the fault?
PDOStatement::fetchColumn Returns a single column from the next row of a result set
You should use fetch() or fetchall() in your case
<?php
$query = $db->query("SELECT * FROM `POS_refund`")->fetchall();
foreach($query as $row){
echo $get_prod_id = $row['prod_id'];
$get_prod_qnt = $row['qnt'];
}
?>
If you want to see if there's records returned you could use count() since fetchall() returns array, then just count your array elemenets
<?php
$query = $db->query("SELECT * FROM `POS_refund`");
$results = $query->fetchall();
if(count($results) > 0){
foreach($results as $row){
echo $get_prod_id = $row['prod_id'];
$get_prod_qnt = $row['qnt'];
}
}else{
echo "No results";
}
?>
I would use a while loop. it would look like this:
while ($row = $query->fetchall) {
echo $get_prod_id = $row['prod_id'];
$get_prod_qnt = $row['qnt'];
}

Fetch multiple records from php/mysql

I am fetching records as follows:
$aResult = array();
$sql = "SELECT notes FROM table WHERE user_id = '$user_id'";
$result = $conn->query($sql);
while($row = mysqli_fetch_array($result)){
$aResult['query_result'] = $row;
}
This returns only the last record in the table. I need to return all records from the sql statement.
change you $aResult['query_result'] = $row; to $aResult['query_result'][] = $row;
You've override the result each time, so you just get one.
It seems your loop constantly overwrites the value and hence you will only ever seen the last row. I think you might see better results if you do something like:
while($row = mysqli_fetch_array($result))
{
$aResult[] = $row;
}
so that each new row gets appended to your array
Try with following code, currently You are initiating the values to the same array key :
$aResult = array();
$sql = "SELECT notes FROM table WHERE user_id = '$user_id'";
$result = $conn->query($sql);
while($row = mysqli_fetch_array($result)){
$aResult['query_result'][] = $row;
}
for more Detail On Array

PHP/MySQL: fetch one result

This is my database:
This is the query:
SELECT * FROM users
Now when I do this:
$query = $connection->query($_GET['query']); // SELECT * FROM users
print_r($query->fetch_assoc());
I get this as output:
Array (
[id] => 3
[username] => karel )
Why does it not output id 4 and username ccscs?
When I a while loop:
while($row = $query->fetch_assoc()){
print_r($row);
}
This is happening because you don't give any order to your query so it automatically get first record. If you want to return last record you can order by id desc as follow
SELECT * FROM users ORDER BY id DESC
If you instead need to retrieve all records you will need to loop throw your records
while($row = $query->fetch_assoc())
{
print_r($row);
}
Based on new op info i would not fetch twice but one as follow
$fields = array();
while($row = $query->fetch_assoc())
{
print_r($row);
$fields[] = $row;
}
fetch_assoc() retrieves a single row from a result set. See here.
mysqli_result::fetch_assoc -- mysqli_fetch_assoc — Fetch a result row
as an associative array
You should use something like this :
while($row = $query->fetch_assoc()){
print_r($row);
}
That's because fetch_assoc() just fetches one row.
To get each row you can do it like this:
while($row = $query->fetch_assoc()){
print_r($row);
}

PHP loop not performing as it should

$query = "SELECT * FROM table WHERE data = '$userinput'";
$row = mysql_query($query);
while ($row = mysql_fetch_array($row))
{
echo $row['data'];
}
Ok So my questions are:
What exactly does mysql_fetch_array return?
AND if my WHERE clause finds multiple matches, shouldn't the loop execute numerous times?
I am running a program and I can only get the first result to print
You're overwriting $row. Instead use a different variable for the query result.
$query = "SELECT * FROM table WHERE data = '$userinput'";
$result = mysql_query($query);
while ($row = mysql_fetch_array($result))
{
echo $row['data'];
}
Try using mysql_fetch_assoc($row)
You should not use the same variable as the index and the loop variable.
$row = mysql_query
while( $row = ...
Replace the first $row with $result

display all cells in a mysql column

I need all values from a table column put into an assoc array. I am having trouble finding the answer.
the table is only six rows deep.
example:
|--id--|--name--|--A--|--B--|--C--|
|--01--|--xl33--| 1.30| 2.45| 4.40|
i would like to get an assoc array for column B
name[xl33]=2.45
name[xl34]=....and so on
The trick is that the form will tell the script which column to fetch. A,B,C,D,E,F OR G
I know i could re-format the table and accomplish what i want but I need it structured the way i have it.( i have left out some columns for simplicity)
function return_column($letter){
$result = mysql_query("SELECT name, $letter FROM example") or die(mysql_error());
while($row = mysql_fetch_assoc( $result )) {
$return[$row['name']] = $row[$letter];
}
return $return;
}
$results = return_column('A');
print_r($results);
Something like :
$col = 'B';
$name = array();
$result = mysql_query("SELECT * FROM table") or die(mysql_error());
while ($row = mysql_fetch_array($result)) {
$name[$row['name']] = $row[$col];
}
This creates an array $name and uses the name column as the key and the $col column for the value ...
You are looking for the mysql_fetch_assoc() function.
Example/
$query = $this->query($YOUR_QUERY);
$returnMap = array();
while ($row = mysql_fetch_assoc($query)) { array_push($returnMap, $row); }
Use mysql_fetch_assoc — Fetch a result row as an associative array.
while ($row = mysql_fetch_assoc($result)) {
$data[$row["column1"]]= $row["column2"];
..............
.........
}

Categories