Reputation: 408
I'm working on a piece of code that's meant to be implemented in Google Charts, however, not all values I'm looking for are being returned.
I need a couple of dates selected from my database, and I select them as follows:
$dates = mysql_fetch_row(mysql_query("SELECT DISTINCT date FROM participants WHERE subdomein='testdomain'")) or die(mysql_error());
Then I use a for-loop to echo them:
for ($i = 0; $i <= count($dates); $i++)
{
echo $dates[0].' ';
}
In my database there are 3 (distinct) dates: 24-03-2013
, 25-03-2013
and 26-03-2013
, however the piece of code returns 2x 24-03-2013
. What am I doing wrong here?
P.S. I also tried a while-loop but that loops infinitely or crashes my page. Besides that, I tested the query by running it in the database itself and it returns the right results, so the query works fine.
Help is much appreciated!
Upvotes: 0
Views: 173
Reputation: 13406
mysql_fetch_row
only returns one record from the actual result of your query. You have to call it several times, ontil it returns null
:
$res = mysql_query(...);
while (($record = mysql_fecth_row($res)) !== null) {
print_r($record[0]);
}
Please note mysql_*
function are deprecated and should not be used anymore, too.
Upvotes: 0
Reputation: 53198
You need to use mysql_fetch_row()
inside a loop in itself:
$result = mysql_query("SELECT DISTINCT date FROM participants WHERE subdomein='testdomain'");
while($date = mysql_fetch_row($result))
{
echo $date[0];
}
You should also note that the mysql_*
family of functions are not deprecated. You should avoid using them if possible, and look into alternatives such as MySQLi or PDO.
Upvotes: 1
Reputation: 32740
Try this: mysql_fetch_row()
fetches only one row, If you need all the rows use mysql_fetch_assoc()
$sql = mysql_query("SELECT DISTINCT date FROM participants WHERE subdomein='testdomain'");
while($dates = mysql_fetch_assoc() ($sql)){
print_r($dates);
}
Ref: http://php.net/manual/en/function.mysql-fetch-row.php
http://www.php.net/manual/en/function.mysql-fetch-assoc.php
Upvotes: 0