Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old March 7th, 2006, 06:55 PM
Ababo
Guest
 
Posts: n/a
Default MySQL PHP problems

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
it:

<?php
$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.

  #2  
Old March 7th, 2006, 07:35 PM
Rafe Culpin
Guest
 
Posts: n/a
Default Re: MySQL PHP problems

In article <1141756865.538422.146370@j52g2000cwj.googlegroups .com>,
dpryde@gmail.com (Ababo) wrote:
[color=blue]
> <?php
> $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[/color]

If $rs isn't a valid result it implies that something went wrong in
creating it. Either in selecting the database or in running the query.
Look at the mysql_error() after each of those.

--
To reply email rafe, at the address cix co uk
  #3  
Old March 7th, 2006, 07:35 PM
David Haynes
Guest
 
Posts: n/a
Default Re: MySQL PHP problems

Ababo wrote:[color=blue]
> 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
> it:
>
> <?php
> $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.
>[/color]

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.

-david-

  #4  
Old March 7th, 2006, 08:05 PM
Jerry Stuckle
Guest
 
Posts: n/a
Default Re: MySQL PHP problems

Ababo wrote:[color=blue]
> 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
> it:
>
> <?php
> $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.
>[/color]

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.
jstucklex@attglobal.net
==================
  #5  
Old March 7th, 2006, 08:15 PM
Adam Plocher
Guest
 
Posts: n/a
Default 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.

  #6  
Old March 7th, 2006, 08:25 PM
Ababo
Guest
 
Posts: n/a
Default 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. :)

  #7  
Old March 7th, 2006, 09:05 PM
Ababo
Guest
 
Posts: n/a
Default 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! :)

 

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles