Page 1 of 1

while and if statements

Posted: Sun Sep 16, 2007 9:00 am
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)

Posted: Sun Sep 16, 2007 9:11 am
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.

Posted: Sun Sep 16, 2007 9:52 am
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?

Posted: Sun Sep 16, 2007 9:55 am
by feyd
How would the return be false? $first_namef is never altered.

Posted: Sun Sep 16, 2007 9:58 am
by kaszu
EDIT: mysql_fetch_array returns only 1 row from database.

Posted: Sun Sep 16, 2007 11:10 am
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.

Posted: Sun Sep 16, 2007 11:42 am
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.

Posted: Sun Sep 16, 2007 11:46 am
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?

Posted: Sun Sep 16, 2007 12:06 pm
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.

Posted: Sun Sep 16, 2007 1:21 pm
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'];

?>

Posted: Sun Sep 16, 2007 1:42 pm
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"

Posted: Sun Sep 16, 2007 1:55 pm
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)

Posted: Sun Sep 16, 2007 2:01 pm
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.

Posted: Sun Sep 16, 2007 2:07 pm
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...

Posted: Sun Sep 16, 2007 3:15 pm
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.