Jump to content

Can't figure out this parse error

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
3 replies to this topic

#1
jackolantern

jackolantern

    Learning Programmer

  • Members
  • PipPipPip
  • 47 posts
I am having a heck of a time trying to figure out why I am getting a parse error on this simple MySQL search script. I am just getting this from a book I am working through, but I have checked about 6 times over the code in the book, and I have exactly what the book has. At this point, I am wondering if the book has an error in the code. Here is the search page:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Book-O-Rama Catalog Search</title>
</head>

<body>
<h1>Book-O-Rama Catalog Search</h1>
<form action="results.php" method="post">
	Choose Search Type:<br />
    <select name="searchtype">
    	<option value="author">Author</option>
        <option value="title">Title</option>
        <option value="isbn">ISBN</option>
     </select>
     <br />
     Enter Search Term:<br />
     <input name="searchterm" type="text" size="40" />
     <br />
     <input type="submit" name="submit" value="Search" />
</form>
</body>
</html>

And here is the script it is calling:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Book-O-Rama Search Results</title>
</head>

<body>
<h1>Book-O-Rama Search Results</h1>	
<?php
	//make short variable names
	$searchtype = $_POST['searchtype'];
	$searchterm = trim($_POST['searchterm']);
	
	if (!$searchtype || !$searchterm) {
		echo "You have not entered enough search terms. Please go back and try again.";
		exit;
	}
	
	if (!get_magic_quotes_gpc()) {
		$searchtype = addslashes($searchtype);
		$searchterm = addslashes($searchterm);
	}
	
	@ $db = new mysqli('*****', '*****', '*****', '*****');
	
	if (mysqli_connect_errno()) {
		echo "Error: Could not connect to database. Please try again later.";
		exit;
	}
	
	$query = "select * from books where ".$searchtype." like '%".$searchterm."%'";
	$result = $db->query($query);
	
	$num_results = $results->num_rows;
	
	echo "<p>Number of books found: ".$num_results."</p>";
	
	for ($i = 0; $i < $num_results; i++) {
		$row = $results->fetch_assoc();
		echo "<p><strong>".($i+1).". Title: ";
		echo htmlspecialchars(stripslashes($row['title']));
		echo "</strong><br />Author: ";
		echo stripslashes($row['author']);
		echo "<br />ISBN: ";
		echo stripslashes($row['isbn']);
		echo "<br />Price: ";
		echo stripslashes($row['price']);
		echo "</p>";
	}
	
	$result->free();
	$db->close();
	
?>
</body>
</html>

And here is the error I am getting:

Quote

Parse error: parse error, expecting `')'' in C:\XAMPP\htdocs\results.php on line 39

Line 39 is:

for ($i = 0; $i < $num_results; i++) {

Although I know that the error could be above or below it. However, I just can't find what I am doing wrong. Thank you for any assistance!

Edited by Jaan, 10 August 2009 - 09:36 AM.
Removed sensitive information


#2
BlaineSch

BlaineSch

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,448 posts
Works fine when I change it to:

for ($i = 0; $i < $num_results; $i++) {

Forgot the $ infront of the last i variable

#3
jackolantern

jackolantern

    Learning Programmer

  • Members
  • PipPipPip
  • 47 posts
Wow, I can't believe I missed that! And I looked directly at that line for about 20 minutes! Thank you very much :) After I fixed that, I was not getting any results from the sample database, so I also realized that my initial variable to hold the database query return was $result, but then along the line I started calling it $results, so I had to fix that, too. And for the record, the book was right, and I was wrong lol.

Thanks again!

#4
BlaineSch

BlaineSch

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,448 posts
Glad I could help, +Rep appreciated =)