while and if statements

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
hgmmy
Forum Commoner
Posts: 25
Joined: Sun Aug 26, 2007 9:16 am

while and if statements

Post by hgmmy »

Ok a problem I just went through and solved on my own was I had two while statements (here's the code, it's the last two while statements)

Code: Select all

<?php //Get the user nickname from the form then use it to find the users id
  $tm_usernick = mysql_real_escape_string($_GET['theusers']);

  $tm_useridq = mysql_query(" SELECT * FROM wp_usermeta WHERE meta_value='$tm_usernick' ") or die("Failed: ".mysql_error());

  $tm_useridf = mysql_fetch_array($tm_useridq);

//get the users first name from the database using the users id
  $first_nameq = mysql_query(" SELECT * FROM wp_usermeta WHERE user_id='{$tm_useridf[user_id]}' AND meta_key='first_name' ") or die("Failed: ".mysql_error()); 

  $first_namef = mysql_fetch_array($first_nameq);

  echo "<table id=\"userinfo\">\n\t<tbody>\n\t\t<tr>\n" ;


//If the user has given their first name create a column with the name first name
  while ($first_namef)
 {
  echo "\t\t\t<th>First Name</th>\n" ;
 } ;

  echo "\t\t</tr>\n\t\t<tr>";

//If the user has given their first name then display it
  while ($first_namef)
 {
  echo "\t\t\t<td> " . $first_namef['meta_value'] . " </td>\n" ;
 } ;
  echo "\t\t</tr>\n\t</tbody>\n</table>";

?>
After making those two statements I went to my page and it just would not load. but after i changed them into an if statement it worked just fine. Why is that? (I hope my explanation/question made sense)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

You have what's called an infinite loop.

Without modifying the variable(s) used in the check condition of a while loop, it will run forever -- provided the condition is true.
hgmmy
Forum Commoner
Posts: 25
Joined: Sun Aug 26, 2007 9:16 am

Post by hgmmy »

But there was only one row with the defined search parameters. So wouldn't it have stopped after looking a second time cause the return would have been false?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

How would the return be false? $first_namef is never altered.
User avatar
kaszu
Forum Regular
Posts: 749
Joined: Wed Jul 19, 2006 7:29 am

Post by kaszu »

EDIT: mysql_fetch_array returns only 1 row from database.
hgmmy
Forum Commoner
Posts: 25
Joined: Sun Aug 26, 2007 9:16 am

Post by hgmmy »

I thought that since the value of $first_namef is mqysl_fetch_array() it would do that until it got nothing back... right?

P.S. I'm just learning so if I'm wrong just crack the whip and point me in the right direction.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

hgmmy wrote:I thought that since the value of $first_namef is mqysl_fetch_array() it would do that until it got nothing back... right?
$first_namef is not a function. It is a value and it will never change until you change it.
hgmmy
Forum Commoner
Posts: 25
Joined: Sun Aug 26, 2007 9:16 am

Post by hgmmy »

So putting $first_namef isn't the same as putting mysql_fetch_array() even though that's what I've assigned to it?
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

hgmmy wrote:So putting $first_namef isn't the same as putting mysql_fetch_array() even though that's what I've assigned to it?
Exactly. Each time you use mysql_fetch_array(), it actually has a pointer that traverses the result set. So, when you run mysql_fetch_array(), it gets the current row, and then points to the next row so that when you call it again, it'll get the next row and continues doing that until there are no rows left. That's why we can use "while ($row = mysql_fetch_array())..." because it will always give us the next row inside of the $row variable.
hgmmy
Forum Commoner
Posts: 25
Joined: Sun Aug 26, 2007 9:16 am

Post by hgmmy »

hmm... very interesting. So if you put "$first_namef = mysql_fetch_array()" like i've done, then put "$first_namef['user_id']" in two or more echos and there are two or more rows in the database, will it progress through the different values as the page progress.

example:

Code: Select all

<?php

/*let's pretend there's three rows that go with this query and that the first row has a value of 1, and the second row 2... for the id*/
  $first_nameq = mysql_query(" SELECT * FROM wp_usermeta WHERE user_id='{admin}' AND meta_key='first_name' ") ;

  $first_namef = mysql_fetch_array($first_nameq) ;

//will this row display 1
  echo $first_namef['user_id'];

//and this one 2
  echo $first_namef['user_id'];

//and this one 3
  echo $first_namef['user_id']; 

//and this one nothing?
  echo $first_namef['user_id'];

?>
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

hgmmy wrote:

Code: Select all

<?php

/*let's pretend there's three rows that go with this query and that the first row has a value of 1, and the second row 2... for the id*/
  $first_nameq = mysql_query(" SELECT * FROM wp_usermeta WHERE user_id='{admin}' AND meta_key='first_name' ") ;

  $first_namef = mysql_fetch_array($first_nameq) ;

//will this row display 1
  echo $first_namef['user_id'];

//and this one 2
  echo $first_namef['user_id'];

//and this one 3
  echo $first_namef['user_id']; 

//and this one nothing?
  echo $first_namef['user_id'];

?>
Again, no. Maybe you don't understand the way that a function works, but a function takes parameters, uses them, and then returns a result. You're code will echo the exact same data three times. Here's what you want to do:

Code: Select all

//will this row display 1
$first_namef = mysql_fetch_array($first_nameq);
  echo $first_namef['user_id'];

//and this one 2
$first_namef = mysql_fetch_array($first_nameq);
  echo $first_namef['user_id'];

//and this one 3
$first_namef = mysql_fetch_array($first_nameq);
  echo $first_namef['user_id']; 

//and this one nothing?
$first_namef = mysql_fetch_array($first_nameq);
  echo $first_namef['user_id'];
A good way to understand functions is to think back to algebra / calculus and how we had f(x).

Code: Select all

/**
 * Same as "f(x) = x^2"
 */
function f($x)
{
    return $x * $x
}

$y = f(0); // 0
$y = f(10); // 100

echo $y; // echoes "100"
hgmmy
Forum Commoner
Posts: 25
Joined: Sun Aug 26, 2007 9:16 am

Post by hgmmy »

Code: Select all

//will this row display 1
$first_namef = mysql_fetch_array($first_nameq);
  echo $first_namef['user_id'];

//and this one 2
$first_namef = mysql_fetch_array($first_nameq);
  echo $first_namef['user_id'];

//and this one 3
$first_namef = mysql_fetch_array($first_nameq);
  echo $first_namef['user_id'];

//and this one nothing?
$first_namef = mysql_fetch_array($first_nameq);
  echo $first_namef['user_id'];
What makes that progress a row each time, cause it still seems like it would display the first row every time.

I've taken no calulus and only like the first year of algebra (I'm behind on algebra)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Each successive call to mysql_fetch_* retrieves the next available record from the query. If there are no further records it will return false to indicate it is done.
hgmmy
Forum Commoner
Posts: 25
Joined: Sun Aug 26, 2007 9:16 am

Post by hgmmy »

ok

I guess I'm done here for today unless anybody else has valuably info I should know on this subjuect. Thanks for educating me...
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

You're using the same result set, so it will go to the next row. I don't know if result sets have an internal pointer, but much like an array, the internal pointer gets moved ahead. Unless it's done.. then it returns false.

This is why you have to use mysql_data_seek() for result sets (and reset() (circumstance providing) for arrays) to move the internal pointer back to the begginning if you want to use the result set again.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
Post Reply