FRIHOSTFORUMSFAQTOSBLOGSDIRECTORY
You are invited to Log in or Register a Frihost Account!

Using SQL to retrieve database information

 


ChrisCh
Hey there. Ok well I've got a MySQL database that contains everybody's username, password, first name, surname etc., and I'm trying to retrieve (and print) a user's first and last name going by their username... For example (hiding database details of course):

Code:
   $db_user = *******
   $db_pass = *******

   //connect to the DB and select the database
   $connection = mysql_connect('localhost', $db_user, $db_pass) or die(mysql_error());
   mysql_select_db('*******', $connection) or die(mysql_error());

   //set up the queries
   $query = "SELECT first_name FROM users
                        WHERE user_name='$curruser' AND password='$currpass'";
        $query2 = "SELECT last_name FROM users
                        WHERE user_name='$curruser' AND password='$currpass'";
         
   //run the queries
   $result = mysql_query($query, $connection) or die('This ChrisCh Student Network database is temporarily unavailable. Please try again later.');
   $result2 = mysql_query($query2, $connection) or die('This ChrisCh Student Network database is temporarily unavailable. Please try again later.');
print $result;
print $result2;

($curruser and $currpass are already declared from a seperate PHP file when the person logs in)

The problem I'm having is that when it prints $result and $result2, it prints Resource id #4 and Resource id #5 respectively. Does anyone know why it's doing this? Any help would be appreciated Smile
Rhysige
Code:
   $db_user = *******
   $db_pass = *******

   //connect to the DB and select the database
   $connection = mysql_connect('localhost', $db_user, $db_pass) or die(mysql_error());
   mysql_select_db('*******', $connection) or die(mysql_error());

   //set up the queries
   $query = "SELECT first_name FROM users
                        WHERE user_name='$curruser' AND password='$currpass'";
        $query2 = "SELECT last_name FROM users
                        WHERE user_name='$curruser' AND password='$currpass'";
         
   //run the queries
   $result = mysql_fetch_object(mysql_query($query, $connection)) or die('This ChrisCh Student Network database is temporarily unavailable. Please try again later.');
   $result2 = mysql_fetch_object(mysql_query($query2, $connection)) or die('This ChrisCh Student Network database is temporarily unavailable. Please try again later.');

print $result->first_name;
print $result2->last_name;


Whagt the query returns is a resource, you have to use this by doing mysql_fetch_object of mysql_fetch_assoc in this case ive used object for you.
ChrisCh
Yay it works! Thanks heaps Very Happy
mathiaus
Not sure how good you are with MySQL but do you know you can use one query rather than two?
Code:
   $db_user = *******
   $db_pass = *******

   //connect to the DB and select the database
   $connection = mysql_connect('localhost', $db_user, $db_pass) or die(mysql_error());
   mysql_select_db('*******', $connection) or die(mysql_error());

   //set up the query
   $query = "SELECT first_name, last_name FROM users WHERE user_name='$curruser' AND password='$currpass'";
         
   //run the query
   $result = mysql_fetch_object(mysql_query($query, $connection)) or die('This ChrisCh Student Network database is temporarily unavailable. Please try again later.');

print $result->first_name.' '.$result->last_name;
Rhysige
Now why didnt I think of telling him that? well to add something of my own extra Razz
If you wanna grab all info from a particular things not just a select few fields then you can use
Code:
SELECT * FROM `table` etc....
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2007 Frihost, forums powered by phpBB.