Jump to content

[SOLVED] query array


jmurch

Recommended Posts

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

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/136375-solved-query-array/
Share on other sites

$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);

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/136375-solved-query-array/#findComment-711544
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/136375-solved-query-array/#findComment-711547
Share on other sites

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;

?>

 

Link to comment
https://forums.phpfreaks.com/topic/136375-solved-query-array/#findComment-711555
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.