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
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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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