simple sql query not working

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ferric
Forum Newbie
Posts: 16
Joined: Wed Mar 17, 2004 3:16 pm

simple sql query not working

Post by ferric »

I'm working on some code that will serve as a function of a bigger script. I couldn't get the function working, so I put the query in it's own php file just to test it... heres the code:

Code: Select all

<?php
$username = "*****";
$password = "*****";
$database = "quahog5news_com_-_information";

mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die("Unable to select database");

$query = "SELECT epname FROM episodeguide WHERE epcode = $epcode";
$output = mysql_query($query);

echo $output;
?>
all I want to do is pull the information from the field "epname" where the epcode is equal to $epcode. $epcode will be passed into the function and is dynamic... what am I doing wrong? this script doesn't return errors, but outputs nothing.
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post by nielsene »

A few things.
1: tried adding an echo $query before line 10. make sure that the expected $epcode is getting filled in.

2. $ouptut will be a result set, not a text string. You'll need something like

Code: Select all

$row = mysql_fetch_row($output);
echo $row["epname"];
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

you can't echo the mysql query like that, it is just a resource, you need to loop over the results it fetches using mysql_fetch_assoc() or the like.

you also might want to echo the query to make sure it's really what you intended.

here's an example of how to use mysql_fetch_assoc:

Code: Select all

$result = mysql_query($query)
   or die(mysql_error());
while($row = mysql_fetch_assoc($result)){
   echo $row['fieldname1'];
}
EDIT: damn it! nielsene beat me while I was posting!
ferric
Forum Newbie
Posts: 16
Joined: Wed Mar 17, 2004 3:16 pm

Post by ferric »

awsome - works good. thanks guys.
Post Reply