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=bob@hotmail.com
ID=2  username=Sue  email=sue@hotmail.com
ID=3  username=Joe  email=joe@hotmail.com
ID=4  username=Moe  email=moe@hotmail.com
ID=5  username=Sam  email=sam@hotmail.com

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 "sue@hotmail.com" right?




If I want to find the username of the row with email=sam@hotmail.com I would enter the following:

[code]
$usernamequery = "SELECT username FROM `people` WHERE `email` LIKE 'sam@hotmail.com'";
$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
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
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.