Page 1 of 1
next row in mysql_fetch_array
Posted: Fri Mar 06, 2009 11:29 am
by medc
suppose i have the following code:
Code: Select all
mysql_select_db($database_stocks, $stocks);
$query_stocks = "some query here";
$stocks1 = mysql_query($query_stocks, $stocks) or die(mysql_error());
$row_stocks = mysql_fetch_assoc($stocks1);
$var1 = $row_stocks['date'];
$var2 = $row_stocks['date'];
$var3 = $row_stocks['date'];
echo $var1, $var2, $var3;
this will output the same date three times, however i need each variable to have the date of the consecutive 3 rows returned by the query. how can i do this??
Re: next row in mysql_fetch_array
Posted: Fri Mar 06, 2009 12:29 pm
by John Cartwright
Code: Select all
while ($row_stocks = mysql_fetch_assoc($stocks1)) {
$date = $row_stocks['date'];
//do something with date
}

Re: next row in mysql_fetch_array
Posted: Fri Mar 06, 2009 12:31 pm
by BomBas
By making a loop.
Code: Select all
mysql_select_db($database_stocks, $stocks);
$query_stocks = "some query here";
$stocks1 = mysql_query($query_stocks, $stocks) or die(mysql_error());
while( $row_stocks = mysql_fetch_assoc($stocks1) )
{
echo $row_stocks['date'];
}
Will print all of the rows.
Re: next row in mysql_fetch_array
Posted: Fri Mar 06, 2009 1:48 pm
by kaisellgren
John Cartwright wrote:Code: Select all
while ($row_stocks = mysql_fetch_assoc($stocks1)) {
$date = $row_stocks['date'];
//do something with date
}
Well, it should be:
Code: Select all
while (($row_stocks = mysql_fetch_assoc($stocks1))) {
$date = $row_stocks['date'];
//do something with date
}
Re: next row in mysql_fetch_array
Posted: Fri Mar 06, 2009 1:52 pm
by John Cartwright
Why?
Re: next row in mysql_fetch_array
Posted: Fri Mar 06, 2009 2:15 pm
by Benjamin
lol, no need for extra parenthesis.

Re: next row in mysql_fetch_array
Posted: Fri Mar 06, 2009 2:28 pm
by kaisellgren
John Cartwright wrote:Why?
Woah...
It's an assignment in a condition. PHP is a language that allows you to write bad code without any side effects. This, however, does not mean it is a good idea to write bad code.
Basically, the proper way would be to:
Code: Select all
$row = mysql_fetch_row(...);
while ($row)
{
...
$row = mysql_fetch_row(...);
}
This could be also done like:
Code: Select all
while (($row = mysql_fetch_row(...) == true))
or if you simplify that:
Code: Select all
while (($row = mysql_fetch_row(...)))
Are extra parenthesis required? Not for making it to work, but in the sake of proper code, yes.
I think above posters do not code C/C++?
Re: next row in mysql_fetch_array
Posted: Fri Mar 06, 2009 2:32 pm
by Benjamin
Well I agree, to an extent..
For instance:
Code: Select all
// ok
if ($x = true)
// redundant
if (($x = true))
// can be confusing, but does work
if (false == $x = true)
// proper
if (false == ($x = true))
Re: next row in mysql_fetch_array
Posted: Fri Mar 06, 2009 2:35 pm
by kaisellgren
I am no one to tell anyone to do anything. I originally said "should", because it should be done that way...
Also, many developers do not care about data type, length, contents and use them improperly.