jmurch Posted December 10, 2008 Share Posted December 10, 2008 I'd like to find out if there is a standard PHP way/function to do this prior to my trying to write it entirely by hand: I have a mysql query that returns a bunch of columns to an array called $auth: $find = mysql_query("SELECT `username`,`password`,`active` FROM users WHERE `username` = '$username' AND `password` = '$password'"); if (!$find) { die('Could not query1:' . mysql_error());} $auth = mysql_fetch_row($find); This give me an indexed array. I'd like to be able to have an array where each item is labeled with the column that initiated the selection so instead of: [1] Joe [2] password [3] 1 I had an array with [username] Joe [password] password [active] 1 I can do this by hand but there must be a way to dynamically get this info from the query as it is executed eh? TIA, Jeff Quote Link to comment Share on other sites More sharing options...
Andy-H Posted December 10, 2008 Share Posted December 10, 2008 $find = mysql_query("SELECT `username`,`password`,`active` FROM users WHERE `username` = '$username' AND `password` = '$password'"); if (!$find) { die('Could not query1:' . mysql_error());} $auth = mysql_fetch_array($find, MYSQL_ASSOC); print_r($auth); Quote Link to comment Share on other sites More sharing options...
jmurch Posted December 10, 2008 Author Share Posted December 10, 2008 perfect thanks andy. I knew that there had to be somethin better. Jeff Quote Link to comment Share on other sites More sharing options...
Psycho Posted December 10, 2008 Share Posted December 10, 2008 You could also use mysql_fetch_assoc(). mysql_fetch_row(): returns numerically indexed array mysql_fetch_assoc(): returns array indexed by the column names mysql_fetch_array(): returns one or both of the above (withthe appropriate switch) All fo this information is readily available in the manual at www.php.net Quote Link to comment Share on other sites More sharing options...
Andy-H Posted December 10, 2008 Share Posted December 10, 2008 You could also use mysql_fetch_assoc(). mysql_fetch_row(): returns numerically indexed array mysql_fetch_assoc(): returns array indexed by the column names mysql_fetch_array(): returns both of the above All fo this information is readily available in the manual at www.php.net and mysql_fetch_object(); to return the results as an object. http://php.net/mysql_fetch_object e.g. <?php $auth = mysql_fetch_object($find); $username = $auth->username; ?> 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.