Bagwaa Posted August 8, 2006 Share Posted August 8, 2006 Howdi,Its been a while since I touched PHP and im having a problem, I have a really basic login script where the username and password are saved in the script, basically they are stored in an assoc array as follows :-$authorized_users = array ( "bill" => "bill", "guest" => "guest" );I have now created a table in a MySQL DB and I can connect to it and pull the information from it, however I cannot get the info into the array .. I was thinking of something like this :-$authorized_users = array (while ($row = mysql_fetch_row($result)) { "$row[0]" => "$row[1]", "guest" => "guest" });But obviously the syntax is all wrong, anyway .. hope im making SOME sense ... :-)ThanksRichard Quote Link to comment Share on other sites More sharing options...
corbin Posted August 8, 2006 Share Posted August 8, 2006 $authorized_users = array ();while ($row = mysql_fetch_row($result)) { authorized_users[] = $row['user'];}Should work... I dont use arrays very often so I'm not sure... Quote Link to comment Share on other sites More sharing options...
Orio Posted August 8, 2006 Share Posted August 8, 2006 Try:[code]<?php//let's say $result holds the resource mysql_query() gave$authorized_users=array();while($row=mysql_fetch_array($result)){$authorized_users[$row['field1']]=$row['field2'];}?>[/code]Is this what you want? (just replace "field1" and "field2" with what you want).Orio. Quote Link to comment Share on other sites More sharing options...
Bagwaa Posted August 8, 2006 Author Share Posted August 8, 2006 thanks for the replies guys ... and orio! many MANY thanks! it worked :-) Quote Link to comment Share on other sites More sharing options...
ToonMariner Posted August 8, 2006 Share Posted August 8, 2006 If you want all the data from your query stored into an associative array (keeping the field names) try this...[code]<?php$qry = "SELECT * FROM `yourtable`";$qry = mysql_query($qry);$arr = array();while ($row = mysql_fetch_assoc($qry)){ foreach ($row as $key => $val) { $arr[$key][] = $value; }}?>[/code]I think that is pretty elegant........ Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.