mysql php problems

Reply

Join Date: Mar 2006
Posts: 2
Reputation: Ababo is an unknown quantity at this point 
Solved Threads: 0
Ababo Ababo is offline Offline
Newbie Poster

mysql php problems

 
0
  #1
Mar 7th, 2006
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 LIMIT 0, 30 ';
$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.
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 3
Reputation: ammcom is an unknown quantity at this point 
Solved Threads: 0
ammcom ammcom is offline Offline
Newbie Poster

Re: mysql php problems

 
0
  #2
Mar 7th, 2006
I think definitly the problem is in SQL statment , check the name of the table and its fields , try adding some debugging code
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 2
Reputation: Ababo is an unknown quantity at this point 
Solved Threads: 0
Ababo Ababo is offline Offline
Newbie Poster

Re: mysql php problems

 
0
  #3
Mar 7th, 2006
Originally Posted by ammcom
I think definitly the problem is in SQL statment , check the name of the table and its fields , try adding some debugging code
I'm pretty positive that the SQL statement is ok. I've tried a number of statements instead of the one I gave, all of which worked when entered directly via phpMyAdmin. I fear that it might be something silly though. That's what it always turns out to be. :-|
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 354
Reputation: Troy is an unknown quantity at this point 
Solved Threads: 5
Troy's Avatar
Troy Troy is offline Offline
Posting Whiz

Re: mysql php problems

 
0
  #4
Mar 7th, 2006
All I can do is expand on what ammcom already told you. Do something like this:
[PHP]

if (!$dbh = mysql_connect ("localhost", "myusername", "mypassword")) {
die ('I cannot connect to the database because: ' . mysql_error());
}

if (!$ret = mysql_select_db ("ababoc_filmlistings")) {
die("db selection failed: ".$mysql_error());
}

$sql = 'SELECT DISTINCT title FROM Listing LIMIT 0, 30 ';
echo "<hr />".$sql."<hr />\n";

if (!$rs = mysql_query($sql)) {
die("db query failed: ".mysql_error());
}

if (!$row = mysql_fetch_row($rs)) {
die("mysql fetch failed: ".mysql_error());
}

echo $row[0];

[/PHP]
If you get an error regarding the query failing, copy and pasted the SQL statement and run it directly against the database--making sure you choose "ababoc_filmlistings" as your db.
Troy Wolf is the author of SnippetEdit. "Website editing as easy as it gets." IX Web Hosting
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 1375 | Replies: 3
Thread Tools Search this Thread



Tag cloud for PHP
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC