MySQL PHP problems

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
Hi everyone. I've only really just started using php and I've been
trying to use it in conjunction with mysql. I keep getting an error
though when trying to access the result set returned by performing a
query on the database. I've performed queries on the database outwith
php, and it correctly returned the correct details. Here's the code for

$dbh=mysql_connect ("localhost", "myusername", "mypassword") or die ('I
cannot connect to the database because: ' . mysql_error());
mysql_select_db ("ababoc_filmlistings");

$sql = 'SELECT DISTINCT title FROM Listing';
$rs = mysql_query($sql);

$row = mysql_fetch_row($rs); // line 24
echo $row[0];


but I end up with the error:

Warning: mysql_fetch_row(): supplied argument is not a valid MySQL
result resource in /home/ababoc/public_html/test/index.php on line 24

I'm really not sure what I could be doing wrong here. Any help would be
much appreciated.

Re: MySQL PHP problems

Kimmo Laine wrote:
Quoted text here. Click to load it

Unless next_page.php generates PHP, the script with this include will
only get HTML.

Quoted text here. Click to load it


    if (isset($_GET['foo'])) {
      echo '<?php echo $_GET[\'foo\']; ?>';
    } else {
      echo '<?php echo \'Not available\'; ?>';

File not found: (R)esume, (R)etry, (R)erun, (R)eturn, (R)eboot

Re: MySQL PHP problems

Ababo wrote:
Quoted text here. Click to load it

Your query most likely threw an error invalidating the result set.

Try this:

$rs = mysql_query($sql);
if( mysql_num_rows($rs) == 0 ) {
   echo mysql_error($dbh);

and see what you get.


Re: MySQL PHP problems

Ababo wrote:
Quoted text here. Click to load it

Check the results of your mysql_select_db() and mysql_query() calls and  
see why one failed.

You should *always* check the results of any call to MySQL and handle  
errors appropriately!

Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.

Re: MySQL PHP problems

Are you absolutely sure that this query is correct?  Try putting:
echo mysql_error();

directly after the mysql_query().  It should be more informative.

Re: MySQL PHP problems

Ahh, okeydoke. I didn't realise there was a mysql_error() function I
could use. Unfortunately my FTP server has decided to act up (always at
the most inopportune of times), but I shall give it a go once it's up
and running again. :)

Re: MySQL PHP problems

Aha! I found out what it was. Turned out that I hadn't given the user
that I was trying to connect with any access privileges to the
database. :-s Whoops! It always turns out to be something stupid.
Thanks for the help though! :)

Site Timeline