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!
<?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)
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?
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.
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.
<?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'];
?>
<?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:
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.
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.