Forum Moderators: coopster
$sql="select userid from users where username='$user' and password='$pass'";
$result = $db->EXECUTE($sql);
while ($userid = mysql_fetch_array($result)){
$usernum = $userid[0];
}
thank you
>>my second array
do you mean from the array (in this case $userarr) that you loaded the mysql results into?
>>assign it’s values to a variable
?isn't that what we just did?
Hook me up with a longer explanation and I would be happy to help out.
I still don't even know of my first post was on point because the query looks like it should only return a single value.
I have a table called users with three fields (userid, username and password) and in it there is users who have same usernames and same passwords (don’t ask why, too long of a story). I have a key “userid” with auto increment.
I have a web page that have a form with two text fields (user, pass).
Example of data in table users:
1*****danny******1234
2*****Robert*****78885
3*****Robert*****78885
4*****Robert*****78885
5*****Jack*******54444
6*****danny******1234
(the *** asterisk is to align the typing and make it easier to view)
so if a user comes to my web page and enters in the user text field ..say [Robert] and enters in the pass text field ..say [78885] he will get a table that will display the following:
2
3
4
the code below I use it to build my table is
<?
$sql="select userid from users where username='$user' and password='$pass'";
$result = $db->EXECUTE($sql);
while ($userid = mysql_fetch_array($result)){
$usernum = $userid[0];
?>
<table>
<tr>
<td><? Echo “$usernum”;?></td>
</tr>
</table>
<?
}
?>
My question is instead of displaying the results in the way above, I want to load such results in another array and use the new array at a latter time.
So then does the first posted loop work? It will do exactly that just change the array bit.
$counter=0;
while ($userid = mysql_fetch_array($result)){
$userarr[$counter] = $userid[0];
$counter++;
}
Important to note that (from php.net on Arrays [php.net])
If you omit a key, the maximum of the integer-indices is taken, and the new key will be that maximum + 1. As integers can be negative, this is also true for negative indices. Having e.g. the highest index being -6 will result in -5 being the new key. If no integer-indices exist yet, the key will be 0 (zero). If you specify a key that already has a value assigned to it, that value will be overwritten.
So, you don't need to use counters. I like them, they make sense to me so I use them regardless.
to redisplay all values in your array you can use the same while setup.
$counter=0;
while (isset($userarr[$counter])){
echo "<table><tr><td>",$userarr[$counter],"</td></tr></table>";
$counter++;
}
should do fine. You could use foreach [php.net] too if you like. Might look like this
foreach ($userarr as $value){
echo "<table><tr><td>",$value,"</td></tr></table>";
}