Really newbie question about databases.
Moderator: General Moderators
Really newbie question about databases.
Hi all
I have one really newbie question....
I have a database of names. Each row has an ID number, first_name etc. If I want to retrieve the first_name for ID #2 for example - what is the syntax?
I already know the ID (primary) and want to retrieve just 1 record. I dont want to start a loop or anything. I cant seem to figure it out.
Thanks
Lawrence.
I have one really newbie question....
I have a database of names. Each row has an ID number, first_name etc. If I want to retrieve the first_name for ID #2 for example - what is the syntax?
I already know the ID (primary) and want to retrieve just 1 record. I dont want to start a loop or anything. I cant seem to figure it out.
Thanks
Lawrence.
-
d3ad1ysp0rk
- Forum Donator
- Posts: 1661
- Joined: Mon Oct 20, 2003 8:31 pm
- Location: Maine, USA
Code: Select all
//connect to DB (you've probably already done this, so i won't type that part)
$sql = "SELECT `first_name` FROM `table_name` WHERE `ID` = '2'";
$name = mysql_result(mysql_query($sql), 0,0);
echo $name;Just 1 more thing 
I actually want to retrieve more than one field, following your advice I am using the following syntax
їphp]
$sql = "SELECT `first_name` FROM `users` WHERE `ID` = '$sponsor'";
$sfname = mysql_result(mysql_query($sql), 0,0);
$sql = "SELECT `last_name` FROM `users` WHERE `ID` = '$sponsor'";
$ssname = mysql_result(mysql_query($sql), 0,0);
echo "$sfname "."$ssname";
ї/php]
I was just wondering if there was an easier way to request the data, maybe from using one mysql_query?
Regards
Lawrence.
?>
I actually want to retrieve more than one field, following your advice I am using the following syntax
їphp]
$sql = "SELECT `first_name` FROM `users` WHERE `ID` = '$sponsor'";
$sfname = mysql_result(mysql_query($sql), 0,0);
$sql = "SELECT `last_name` FROM `users` WHERE `ID` = '$sponsor'";
$ssname = mysql_result(mysql_query($sql), 0,0);
echo "$sfname "."$ssname";
ї/php]
I was just wondering if there was an easier way to request the data, maybe from using one mysql_query?
Regards
Lawrence.
?>
- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK
You can combine those two queries to:
You could then continue to use mysql_result() or instead use [php_man]mysql_fetch_assoc[/php_man]():
Mac
Code: Select all
SELECT first_name, last_name FROM users WHERE ID = '$sponsor'Code: Select all
$result = mysql_query($sql) or die(mysql_error().'<p>'.$sql.'</p>');
$row = mysql_fetch_assoc($result);
echo $row['first_name'].' '.$row['last_name'];- mrvanjohnson
- Forum Contributor
- Posts: 137
- Joined: Wed May 28, 2003 11:38 am
- Location: San Diego, CA
Or you can select everything for that record and just display the fields you want using this as your Select
Code: Select all
SELECT * FROM users WHERE ID = '$sponsor'- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK
But this is not the preferred option for production code - much better IMO is to specifically name the fields you are trying to return as it makes it much easier to deal with changes to the database. Plus if you have 15 columns and only want to use data from 5 of them you it's a waste to return everything.mrvanjohnson wrote:Or you can select everything for that record and just display the fields you want using this as your Select
Code: Select all
SELECT * FROM users WHERE ID = '$sponsor'
Mac