Page 1 of 1

Parse error trying to display MySQL DB data

Posted: Sun Sep 14, 2003 5:03 pm
by RobertGonzalez
I am new to PHP. Can someone tell me what is wrong with this code? It does everything it is supposed to do, except that when it loads without passed variables, I get the following error message:
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/eautospot/www/reviews2.php on line 67

The page is at http://www.eautospot.com/reviews2.php. Here is the code:

Code: Select all

<html> 
<head> 
<title> Review Lister TEST </title> 
</head> 
<body> 
<?php 
 if (isset($_GET&#1111;"addreview"])): // If the user wants to add a record
?> 

<form action="<?=$_SERVER&#1111;"PHP_SELF"]?>" method="post"> 
<p>Enter your first name:<br /> 
<input type=text name="FirstName" />
<p>Enter your last name:<br /> 
<input type=text name="LastName" />
<p>Enter your email:<br /> 
<input type=text name="Email" />
<p>Give your review a title:<br /> 
<input type=text name="Title" />
<p>Enter your review (HTML ok):<br /> 
<textarea name="Review" rows="10" cols="40" wrap></textarea><br /> 
<input type="submit" name="submitreview" value="SUBMIT" /></p> 
</form> 

<?php 
  else:
 // Connect to the database server 
 $dbcnx = @mysql_connect("localhost", "XXXXXXXX", "********"); 
 if (!$dbcnx) &#123; 
   echo( "<p>Unable to connect to the " . 
         "database server at this time.</p>" ); 
   exit(); 
 &#125; 
 // Select the database 
 if (! @mysql_select_db("XXXXXXXX") ) &#123; 
   echo( "<p>Sorry, but I was not able to get into your " . 
         "database at this time.</p>" ); 
   exit(); 
 &#125;

   // If a post has been made, add it
  if (isset($_POST&#1111;"submitreview"])) &#123;
    $firstname = $_POST&#1111;"FirstName"];
    $lastname = $_POST&#1111;"LastName"];
    $email = $_POST&#1111;"Email"];
    $title = $_POST&#1111;"Title"];
    $review = $_POST&#1111;"Review"];
  $sql = "INSERT INTO eautospot_reviews SET
          FirstName='$firstname',
          LastName='$lastname',
          Email='$email',
          Title='$title',
          Review='$review'";
   if (@mysql_query($sql)) &#123;
   echo ("<p>Your review has been posted.</p>");
   &#125; else &#123;
   echo ("<p>Sorry, your review was not posted because of the following: " . mysql_error() . "</p>");
   &#125;
  &#125;

  //If a user elects to view a review
  if (isset($_GET&#1111;"readreview"])) &#123;
  $readid = $_GET&#1111;"readreview"];
  $result = @mysql_query("SELECT ID, Email, Title, Review FROM eautospot_reviews WHERE ID=$readid");
  &#125;
  //Show an individual review
  //THE NEXT LINE IS WHERE THE ERROR COMES IN
  while ($row = mysql_fetch_array($result)) &#123;
  $readidtext = $row&#1111;"ID"];
  $emailtext = $row&#1111;"Email"];
  $titletext = $row&#1111;"Title"];
  $reviewtext = $row&#1111;"Review"];
  echo ("<B>$titletext</B><BR>$reviewtext<P><A HREF="mailto:$emailtext">Email the reviewer</A></P>");
   &#125;
   echo("<p> Here is a list of all of our reviews: </p>"); 
   // Request the ID and titles of all reviews 
   $result = @mysql_query("SELECT ID, Title FROM eautospot_reviews"); 
   if (!$result) &#123; 
     echo("<p>Error performing query: " . mysql_error() . "</p>"); 
     exit(); 
   &#125; 
   // Display the title of each review with alink to the review
   while ( $row = mysql_fetch_array($result) ) &#123; 
     $reviewid = $row&#1111;"ID"]; 
     $reviewtitle = $row&#1111;"Title"];
     echo("<B><a href='$PHP_SELF?readreview=$reviewid'>$reviewtitle</a></p>"); 
   &#125; 
   // When clicked, this link will load this page with a submit form
   echo("<p><a href='$PHP_SELF?addreview=1'>Add your own review!</a></p>"); 

endif;
?> 
</blockquote> 
</body> 
</html>
Any help would be appreciated. Thanks.

Posted: Sun Sep 14, 2003 5:16 pm
by JAM
On row 63, you are getting some results. Ar you sure that you are actually recieving any hits in that query?

If you search for something "WHERE ID=$readid" but nothing is found, you'll get this error.

You could add some code to verify this on row 66 or so, that

Code: Select all

if (mysql_num_rows($result) > 0) {
 ... your code ...
}
...to help you pass this error.

Posted: Mon Sep 15, 2003 4:33 am
by twigletmac
You need to add some error handling here:

Code: Select all

$result = @mysql_query("SELECT ID, Email, Title, Review FROM eautospot_reviews WHERE ID=$readid");
as something about this query is failing. Try:

Code: Select all

$sql = "SELECT ID, Email, Title, Review FROM eautospot_reviews WHERE ID=$readid";
$result = mysql_query($sql) or die(mysql_error().'<p>'.$sql.'</p>');
to see what MySQL says about the problem.

Mac

Error still there...

Posted: Mon Sep 15, 2003 10:09 am
by RobertGonzalez
Thanks for the replies. I tried adding the error handling and it didn't change the parse error warning. I think the problem is in the mysql_fetch_array() statement, but I don't know why.

What I want the page to do is:
When loaded, display all review Titles as hyperlinks.
If a Title hyperlink is clicked, the page reloads and displays the full text of the review.
If the "Add a review" link is clicked at the bottom of the page, then the page reloads with a form to add a review.

All of these function work except when the page loads for the first time, I get the parse error on the mysql_fetch_array() at line 67. It still loads the list of review titles as links, and the links work. But the parse error line still shows up.

Posted: Mon Sep 15, 2003 10:23 am
by JayBird
it is because, if you load the page for the first time, $_GET["readreview"] and $_POST["submitreview"] have not been set so non of the if statements are executed and therefor, when you reach the mysql_fetch_array() statement, $result has not been set.

try changing the

Code: Select all

while ($row = mysql_fetch_array($result)) {
to

Code: Select all

$result = @mysql_query("SELECT ID, Email, Title, Review FROM eautospot_reviews");
while ($row = mysql_fetch_array($result)) {
Mark

PROBLEM SOLVED!

Posted: Mon Sep 15, 2003 3:05 pm
by RobertGonzalez
:D :!:
Thanks for all your help. I made a very stupid mistake. I closed my "if" bracket too early:

Code: Select all

&lt;?php
  //If a user elects to view a review
  if (isset($_GET&#1111;"readreview"])) {
  $readid = $_GET&#1111;"readreview"];
  $sql = "SELECT ID, Email, Title, Review FROM eautospot_reviews WHERE ID=$readid"; 
  $result = mysql_query($sql) or die(mysql_error().'&lt;p&gt;'.$sql.'&lt;/p&gt;');
  }  
  //Show an individual review
  while ($row = mysql_fetch_array($result)) {
  $readidtext = $row&#1111;"ID"];
  $emailtext = $row&#1111;"Email"];
  $titletext = $row&#1111;"Title"];
  $reviewtext = $row&#1111;"Review"];
  echo ("&lt;B&gt;$titletext&lt;/B&gt;&lt;BR&gt;$reviewtext&lt;P&gt;&lt;A HREF="mailto:$emailtext"&gt;Email the reviewer&lt;/A&gt;&lt;/P&gt;");
   }
?&gt;
Should have been:

Code: Select all

<?php
  //If a user elects to view a review
  if (isset($_GET["readreview"])) {
  $readid = $_GET["readreview"];
  $sql = "SELECT ID, Email, Title, Review FROM eautospot_reviews WHERE ID=$readid"; 
  $result = mysql_query($sql) or die(mysql_error().'<p>'.$sql.'</p>');
  
  //Show an individual review
  while ($row = mysql_fetch_array($result)) {
  $readidtext = $row["ID"];
  $emailtext = $row["Email"];
  $titletext = $row["Title"];
  $reviewtext = $row["Review"];
  echo ("<B>$titletext</B><BR>$reviewtext<P><A HREF="mailto:$emailtext">Email the reviewer</A></P>");
   }
  }
?>
Thanks for all your help and insight. It is most appreciated.