Connecting Tech Pros Worldwide Help | Site Map

MySQL PHP problems

Ababo
Guest
 
Posts: n/a
#1: Mar 7 '06
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.

Rafe Culpin
Guest
 
Posts: n/a
#2: Mar 7 '06

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
David Haynes
Guest
 
Posts: n/a
#3: Mar 7 '06

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-

Jerry Stuckle
Guest
 
Posts: n/a
#4: Mar 7 '06

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
==================
Adam Plocher
Guest
 
Posts: n/a
#5: Mar 7 '06

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.

Ababo
Guest
 
Posts: n/a
#6: Mar 7 '06

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. :)

Ababo
Guest
 
Posts: n/a
#7: Mar 7 '06

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! :)

Closed Thread