Forum Moderators: coopster

Message Too Old, No Replies

loading results into an array

loading a while loop statement into an array (PHP)

         

Mitch888

6:42 pm on Apr 17, 2003 (gmt 0)

10+ Year Member



hi,
I need to load the results ($usernum = $userid[0]) of the following while statement into an array so I can use it at a later time.

$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

jatar_k

6:59 pm on Apr 17, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I would think that query would only return a single field value but when I loop through results I do it like so

$counter=0;
while ($userid = mysql_fetch_array($result)){
$userarr[$counter] = array($userid[0],$userid[1]$userid[2]);
$counter++;
}

something like that

Mitch888

7:58 pm on Apr 17, 2003 (gmt 0)

10+ Year Member



So if I want to fetch the results from my second array, what would I do? How would I put it in a while loop and assign it’s values to a variable.

Thank you

jatar_k

9:57 pm on Apr 17, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I am not totally sure if I am getting it.

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

Mitch888

10:24 pm on Apr 17, 2003 (gmt 0)

10+ Year Member



The user & pass is kind of confusing.

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.

jatar_k

10:44 pm on Apr 17, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



crystal clear, thx

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>";
}

Mitch888

11:01 pm on Apr 17, 2003 (gmt 0)

10+ Year Member



Oh man, if you were woman I would kiss you. It worked like a charm. You can’t believe how much time you saved me. I actually will enjoy this holidays (happy holidays by the way)

Thank you Thank you
Thank you
Thank you
Thank you
:)

jatar_k

11:15 pm on Apr 17, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



hehe, glad I could help and make your holiday enjoyable. ;)