I want to put the results in variables seperately but for that I will need to be able to call them separate. Right now I have this query that retrieves 6 results (tested with mysqli_num_rows).
But when I print it, it will only shows the first row from my 6 results.
$result = mysqli_query ($con, $query) or die("FOUT: " . mysqli_error($con));
echo "<p>mysqli_num_rows($result)</p>";
$row = mysqli_fetch_row($result);
print_r($row);
mysqli_free_result($result);
Results from
print_r($row) =
Array ( [0] => Iran [1] => 28 )
To get all rows you will need to do something like:
$rows = array();
while($row = mysqli_fetch_row($result)) {
$rows[] = $row;
}
// $rows will now contain all rows in the result set
Your function, mysqli_fetch_row(), only returns a single row result:
http://php.net/manual/en/mysqli-result.fetch-row.php
Try looping through like this:
while ($row = mysqli_fetch_row($result) {
// Do something
}
Thanks,
Andrew
You should use while
while ($row = mysqli_fetch_row($result)){
print_r($row);
//do more stuff...
}
You can use fetch_assoc instead of fetch_row to have logical array keys
Try this it will work :
$result = mysqli_query ($con, $query) or die("FOUT: " . mysqli_error($con));
echo "<p>mysqli_num_rows($result)</p>";
$rows = array();
$row = mysqli_fetch_row($result);
do
{
$rows[] = $row;
}while($row = mysqli_fetch_row($result))
mysqli_free_result($result);
Related
How do I return all the row values for a given id?
require('dbc.php');
mysql_select_db($db);
$result = mysql_query("SELECT * FROM about WHERE id=".$_GET['id']);
$row = mysql_fetch_assoc($result);
echo $row['content']; // return all values instead of just content
You can use mysql_fetch_row and then implode the array:
echo implode(",",mysql_fetch_row($result));
The following will work:
while ($row = mysql_fetch_assoc ($result) ) {
foreach($row as $column => $value){
echo "Row: $column - $value<br />";
}
}
But, I would suggest you move to MySQLi, for security and performance purposes. Once you do, you can use:
$all_rows = mysqli_fetch_all ($result, MYSQLI_ASSOC);
You could loop through this or call implode on it to return the values.
function GetVideoInfo( $video_id, $user_id )
{
$result = mysql_query("SELECT * FROM `mytable`
WHERE
video_id = '$video_id'
AND
user_id = '$user_id'")
or die( mysql_error() );
return mysql_fetch_array( $result );
}
$videoRecepients = $viddler_custom->GetVideoRecepients( $video_details['id'] );
echo "<pre>";
print_r($videoRecepients);
echo "</pre>"
When I try using print_r, it only results a single row in the table. My expected result should have 2 results. I am 100% sure that my query is correct, so that is not the problem. I'm thinking that maybe it's on my mysql_fetch_array that is wrong.
Your help would be greatly appreciated and rewarded! Thanks! :)
All of the fetch functions will return a single row, you will need to loop until the result is empty like this (snippet from php.net).
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
printf("ID: %s Name: %s", $row["id"], $row["name"]);
}
From the example on the manual page, mysql_fetch_array returns the information on the current pointer of the $result object. This will mean you want to loop through the result until you've fetched everything.
while ($row=mysql_fetch_array($result)) {
$set[] = $row;
}
print_r($set);
You need to put the mysql_fetch_array($result) in a loop
while($row = mysql_fetch_array($result))
{
// do something with $row
}
I have a problem iterating through an sql query:
$result = mysql_query("SELECT * FROM transactions");
while($row = mysql_fetch_array($result)) {
// this returns 3 rows
foreach ($row as $values)
{
//fputcsv($a_csv, $values;
echo $values;
}
}
The script iterates fine but it appears to be going through each row twice. So what I receive in output is the following:
value1value1value2value2value3value3
I'm not sure why this is - could anyone explain please?
Thankyou
mysql_fetch_array fetches both the named & the numerical keys. Use either mysql_fetch_assoc or mysql_fetch_row.
$result = mysql_query("SELECT * FROM transactions");
//return an associative array
while($row = mysql_fetch_assoc($result)) {
// this returns 3 rows
$values = "{$row["name_of_column1"]}, {$row["name_of_column2"]}, {$row["name_of_column3"]}";
//fputcsv($a_csv, $values;
//print the whole row array
print_r($row);
//echo value in format value1, value2, value3
echo $values;
}
You need to access $row like this $row[0] And $row shouldn't be in a foreach() itself unless it too is some kind of array you need to iterate through.
$result = mysql_query("SELECT * FROM transactions");
while($row = mysql_fetch_row($result))
{
echo $row[0];
echo $row[1];
// ... etc.
}
$query = "SELECT * FROM table";
$result = mysql_query($query, $db);
$all = mysql_fetch_assoc($result);
echo mysql_num_rows($result) . ":" . count($all);
This returns
2063:7
I have not used count before, so I'm not 100% sure it's not counting the table columns. It's late and I might be going nuts.
Here's another example of what's happening:
$result = mysql_query($query, $db);
echo "Rows: " . mysql_num_rows($result) . " <BR />";
$player_array = mysql_fetch_assoc($result);
echo "<pre>";
print_r($player_array);
echo "</pre>";
Which outputs:
Rows: 9
Array
(
[playerID] => 10000030
)
TL;DR: I submit queries which return multiple rows, but fetch_array only gives me a small portion of those rows in the resulting array.
mysql_fetch_assoc returns only one row in once you have to use loop to retrieve all rows
while($row = mysql_fetch_assoc($result))
{
print_r($row);
}
Every call to mysql_fetch_assoc($result); gives you one row of the result set:
(from the documentation)
mysql_fetch_assoc — Fetch a result row as an associative array
Returns an associative array that corresponds to the fetched row and moves the internal data pointer ahead. mysql_fetch_assoc() is equivalent to calling mysql_fetch_array() with MYSQL_ASSOC for the optional second parameter. It only returns an associative array.
You have to use the function in a loop:
$all = array();
while(($row = mysql_fetch_assoc($result))) {
$all[] = $row;
}
The example in the document shows how it is done.
mysql_fetch_assoc doesn't work that way, you need to call it multiple times to get all rows. Like this:
while ($row = mysql_fetch_assoc($db_result))
{
print_r($row);
}
When I query my database with the following in my file, search.php, it only returns the first result it comes across.
$qry = "SELECT business_id FROM business WHERE zip like '%91326%'";
$rs = mysql_query($qry);
$rec = mysql_fetch_array($rs);
echo $session->showContents($rec);
showContents is just a utility function...
function showContents($array)
{
echo "<pre>";
print_r($array);
echo "</pre>";
}
showContents returned this:
Array
(
[0] => 3
[business_id] => 3
)
The crazy thing is, when I put the same query in sqlbuddy it gives me:
business_id
3
5
6
I am at a loss
mysql_fetch_array fetches only a single row. You want to use it several times to build an array with the entire result set:
$rec = array();
while(($row = mysql_fetch_array($rs)) !== FALSE) {
$rec[] = $row;
}
If you just want the ID's you want to select the ID:
$rec = array();
while(($row = mysql_fetch_array($rs)) !== FALSE) {
$rec[] = $row[0];
}
Try this:
$qry = "SELECT business_id FROM business WHERE zip like '%91326%'";
$rs = mysql_query($qry);
while ($rec = mysql_fetch_array($rs)) {
echo $session->showContents($rec);
}
That's because mysql_fetch_array only fetches a single row from the result set.
Typically you use it like this (from the manual):
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
printf("ID: %s Name: %s", $row[0], $row[1]);
}