PDA

View Full Version : Selecting single entry from db



koolkrasher
December 18th, 2007, 02:26 PM
Im having a little problem selecting a single entry from a db><
heres my code



<?php
$name= mysql_query(" SELECT 'team_name' FROM `team` WHERE team_id =1");
if (!$name){
die("Database query failed: " . mysql_error());
}


echo "$name";


?>


i ran it through the sql console in phpmyadmin and it worked fine but when i jumped over into php and tried executing it echos out the following

Resource id#5

any ideas what im dooing wrong?

deletedUser2352352
December 18th, 2007, 02:29 PM
is it the space before the SELECT statement?

koolkrasher
December 18th, 2007, 02:39 PM
Nope

deletedUser2352352
December 18th, 2007, 02:41 PM
Does this work?



<?php
$name = mysql_query("SELECT team_name FROM team WHERE team_id =1");
if (!$name)
{
die("Database query failed: " . mysql_error());
}
echo "$name";
?>

Seb Hughes
December 18th, 2007, 02:54 PM
Put a LIMIT 1 at the end of your MySQL qquery.

koolkrasher
December 18th, 2007, 03:34 PM
i added the LIMIT 1 to the end of the sql statment and i ran it through php my admin and it didnt give me any errors but, under the sql field it usualy shows u the row it pulls out this time it only gave me

team_name
___________
team_name

this is my new query



<?php
$name=mysql_query("SELECT 'team_name' FROM `team` WHERE team_id =1 LIMIT 1");
if (!$name){
die("Database query failed: " . mysql_error());
}


echo ""$name";


?>

Bmoeb
December 18th, 2007, 04:10 PM
<?php
$name=mysql_query("SELECT team_name FROM team WHERE team_id =1");
if (!$name){
die("Database query failed: " . mysql_error());
} else{
$r = mysql_fetch_array($name);
echo $r['team_name'];
}
?>


Try this...

koolkrasher
December 18th, 2007, 05:35 PM
oh man i am such an idiot....... the col namewas "name" not team name it works so far but it only displays the first item in the array. how would i tell it to echo say the 5th item in the array?

i tried

echo "$r[5]";

and nothing gets echoed out

MTsoul
December 18th, 2007, 06:01 PM
You need to use mysql_fetch_array, like Bmoeb said. It converts resources returned from MySQL into PHP variables.

Bmoeb
December 18th, 2007, 06:45 PM
<?php
$i=0;
$name=mysql_query("SELECT name FROM team WHERE team_id =1");
if (!$name){
die("Database query failed: " . mysql_error());
} else{
//while will loop through entire table and show all team names.
while($r = mysql_fetch_array($name)){
if($i==4){
//if $i is 5th row 0-4 echo name
//if you want all rows remove if($i==4){ and } below..
echo $r['name'];
}
$i++;
//increment i
}
}
?>

If team_id is only in the table once it will only show the one record that has that team_id. If you want it to show more change
$name=mysql_query("SELECT name FROM team WHERE team_id =1");
to
$name=mysql_query("SELECT name FROM team");

alexgeek
December 18th, 2007, 07:39 PM
You need to use mysql_fetch_array, like Bmoeb said. It converts resources returned from MySQL into PHP variables.
mysql_fetch_assoc is easier to use, and mysql_fetch_object is easier again :tb:

koolkrasher
December 19th, 2007, 08:50 AM
thanks for the help guys apreciate it a lot!