Page 1 of 1

simple sql query not working

Posted: Mon Jul 25, 2005 2:35 pm
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.

Posted: Mon Jul 25, 2005 2:40 pm
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"];

Posted: Mon Jul 25, 2005 2:42 pm
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!

Posted: Mon Jul 25, 2005 3:20 pm
by ferric
awsome - works good. thanks guys.