PHP/MySQL: fetch one result - php

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);
}

Related

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

How to store a single selected row value in variable?

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'];
}

SQL query only display first result

the code below display the row 116 twice, it won't display row 118. Any idea how to solve this issue?
$data = mysql_query("SELECT * FROM item WHERE dcid IN('116','118')")
or die(mysql_error());
$info = mysql_fetch_array($data);
foreach ($info as $item) {
echo($item);
}
mysql_fetch_array only fetches a single row. Typically it is used in a while loop to cycle through all the results.
To continue your example above:
$data = mysql_query("SELECT * FROM item WHERE dcid IN('116','118')") or die(mysql_error());
while ($item = mysql_fetch_array($data)) {
echo($item)
}
your query must be as:
$data = mysql_query("SELECT * FROM item WHERE dcid IN('116','118')") or die(mysql_error());
while ($item = mysql_fetch_array($data)) {
echo $item['column_name1'];
} echo $item['column_name2'];
mysql_fetch_array returns a single row in an array like this that contains both an associative array and a regular numeric-keyed result set of your row.
0=> column,
column=>column
Thats why it returns twice in foreach.Use it like this
mysql_fetch_array($result, MYSQL_ASSOC);
Also, if dcid is your key and it's autoincrementing ( ID for rows ) you can also use LIMIT 116,118.
For more info: http://php.about.com/od/mysqlcommands/g/Limit_sql.htm
use group by as this
$data =mysql_query("SELECT * FROM item WHERE dcid IN('116','118') group by dcid")
or die(mysql_error());

JOIN query only returning 1st row

I'm trying to retrieve multiple rows from joining two tables where store.itemid = item_list.id.
$query = "SELECT s.price, il.*
FROM store s LEFT JOIN item_list il ON s.itemid = il.id";
I then have:
if($result = $conn->($query)) {
$array = $result->fetch_array(MYSQLI_ASSOC);
}
With my current code, the query is only retrieving the first row from the 'store' table. I have made certain that there should definitely be more than one row to return.
print_r($array) shows:
Array ( [price] => 400 [id] => 5 [name] => Computer )
That's because you are only running fetch_array() once. You probably need to run it in a loop, e.g.:
if ($result = $conn->query($query)) {
while ($array = $result->fetch_array(MYSQLI_ASSOC)) {
// do something with $array
}
}
instead of this:
$array = $result->fetch_array(MYSQLI_ASSOC);
use this:
while($row = $result->fetch_array(MYSQLI_ASSOC)){
//put your code here!
}

How can I get the last ten rows in a Mysql database table?

I use this
$query = "SELECT * FROM info ORDER BY id DESC limit 10";
$result = #mysql_query( $query );
$row = mysql_fetch_array($result);
print_r($row);
but it gets just the last row
mysql_fetch_array does not fetch an array of rows.
It fetches an array of columns from a single row.
To get all rows, you have to run it in a loop:
$query = "SELECT * FROM info ORDER BY id DESC limit 10";
$result = #mysql_query( $query );
while ($row = mysql_fetch_array($result))
print_r($row);
The query is correct. If you seem to be only getting one row, it's a factor external to the query causing it: either you only have one row in the table, or your application logic is hosed so that it looks like you only have one row.
Edit: Yeah, now that you've posted your code, we can see that it's that your application logic is hosed. Try this:
$result = mysql_query($query);
$rows = array();
while($row = mysql_fetch_array($result))
$rows[] = $row;

Categories