xProteuSx Posted December 1, 2006 Share Posted December 1, 2006 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.comID=2 username=Sue email=sue@hotmail.comID=3 username=Joe email=joe@hotmail.comID=4 username=Moe email=moe@hotmail.comID=5 username=Sam email=sam@hotmail.comIf 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 ... Quote Link to comment Share on other sites More sharing options...
joshi_v Posted December 1, 2006 Share Posted December 1, 2006 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.RegardsJoshi. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.