next row in mysql_fetch_array

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
medc
Forum Newbie
Posts: 6
Joined: Mon Mar 02, 2009 3:56 pm

next row in mysql_fetch_array

Post 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??
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: next row in mysql_fetch_array

Post by John Cartwright »

Code: Select all

 
while ($row_stocks = mysql_fetch_assoc($stocks1)) {
   $date = $row_stocks['date'];
   //do something with date
} 
:)
BomBas
Forum Commoner
Posts: 41
Joined: Wed Mar 04, 2009 1:04 pm

Re: next row in mysql_fetch_array

Post 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.
User avatar
kaisellgren
DevNet Resident
Posts: 1675
Joined: Sat Jan 07, 2006 5:52 am
Location: Lahti, Finland.

Re: next row in mysql_fetch_array

Post 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
}
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: next row in mysql_fetch_array

Post by John Cartwright »

Why?
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: next row in mysql_fetch_array

Post by Benjamin »

lol, no need for extra parenthesis. :drunk:
User avatar
kaisellgren
DevNet Resident
Posts: 1675
Joined: Sat Jan 07, 2006 5:52 am
Location: Lahti, Finland.

Re: next row in mysql_fetch_array

Post 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++?
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: next row in mysql_fetch_array

Post 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))
 
User avatar
kaisellgren
DevNet Resident
Posts: 1675
Joined: Sat Jan 07, 2006 5:52 am
Location: Lahti, Finland.

Re: next row in mysql_fetch_array

Post 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.
Post Reply