Jump to content

How to Query an DB


xProteuSx

Recommended Posts

I am really confused as to how to query a MySQL database.  I have had several examples that work, each somehow different, yet when I try to adapt these examples to new situations, I cannot get them to work.  Really, I am getting frustrated.  Maybe someone can explain this in idiot-proof terms, so that I may be enlightened ... (PLEASE!!!)

So here is the sample table named PEOPLE (3 columns (id, username, email, respectively), 5 rows):


ID=1  username=Bob  [email protected]
ID=2  username=Sue  [email protected]
ID=3  username=Joe  [email protected]
ID=4  username=Moe  [email protected]
ID=5  username=Sam  [email protected]

If I want to find the e-mail of the row with ID=2 I would enter the following:

[code]
$emailquery = "SELECT email FROM `people` WHERE `id` LIKE '2'";
$email_result = mysql_query($emailquery) or die ("The query caused the following error:<br><br>" . mysql_error());
$email_given = mysql_fetch_assoc ($email_result);
echo $email_given['users_email'];
[/code]

This should have the output "[email protected]" right?




If I want to find the username of the row with [email protected] I would enter the following:

[code]
$usernamequery = "SELECT username FROM `people` WHERE `email` LIKE '[email protected]'";
$username_result = mysql_query($usernamequery) or die ("The query caused the following error:<br><br>" . mysql_error());
$username_given = mysql_fetch_assoc ($username_result);
echo $username_given['users_username'];
[/code]

This should have the output "Sam" right?

Please, I am stuck and I cannot figure this out ...
Link to comment
https://forums.phpfreaks.com/topic/29058-how-to-query-an-db/
Share on other sites

Well! The querying part weas right.but not displaying the result to browser.

[code]
<?php

$emailquery = "SELECT email FROM `people` WHERE `id` LIKE '2'"; # Here it will exact result if you use WHERE id = '2'.
$email_result = mysql_query($emailquery) or die ("The query caused the following error:<br><br>" . mysql_error());
$email_given = mysql_fetch_assoc ($email_result);
echo $email_given['users_email']; # this is where you are doing mistake.

# it should be
echo $email_given['email']; # this 'email' is the value ,for what you are querying the DB. you should use the same name for the fieldnames.

[/code]
?>
the same will do for second part.


Regards
Joshi.
Link to comment
https://forums.phpfreaks.com/topic/29058-how-to-query-an-db/#findComment-133201
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.