[SOLVED]LXF 170 "The LAMP stack" Errors

Comments, suggestions and questions about Linux Format magazine and the coverdiscs

Moderators: ChrisThornett, LXF moderators

[SOLVED]LXF 170 "The LAMP stack" Errors

Postby gdawg » Tue Jul 16, 2013 4:02 pm

Hi, while trying to run http://localhost/booksearch.html I get the following error
Fatal error: Call to a member function bind_result() on a non-object in /var/www/html/booksearch.php on line 41 Call Stack: 0.0004 334840 1. {main}() /var/www/html/booksearch.php:0.
Here is the code:
Code: Select all
  <!DOCTYPE html>
<html>
<head>
 <title>Library Book Search</title>
</head>
<body>
<h3>Book Search Results</h3><br>
<hr>
<?php
// Get data from form
$searchtitle = trim($_REQUEST['searchtitle']);
$searchauthor = trim($_REQUEST['searchauthor']);
if(!$searchtitle && !$searchauthor) {
 echo "You must specify either a title or an author";
 exit();
}
$searchtitle = addslashes($searchtitle);
$searchauthor = addslashes($searchauthor);


// Open the database
@ $db = new mysqli('localhost', 'root', '-t9m3s4e8d0*', 'library');
if($db->connect_error) {
        echo "could not connect:" .$db->connect_error;
        exit();
}
// Build the query. Users are allowed to search on title,
// author, or both
$query = "select * from books";
if($searchtitle && !$searchauthor) { // Title search only
 $query = $query . " where title like '%" . $searchtitle . "%'";
}
if(!$searchtitle && $searchauthor) { // Author search only
 $query = $query . " where author like '%" . $searchauthor . "%'";
}
if($searchtitle && $searchauthor) { // Title and Author search
  $query = $query . "where title like '%" . $searchtitle . "%'
and
author like '%" . $searchauthor . "%'"; // unfinished
}
// Run the query using bound result parameters
$stmt = $db->prepare($query);
$stmt->bind_result($bookid, $title, $author, $onloan, $duedate, $borrowerid);
$stmt->execute();
echo "<table border=1>";
while ($stmt->fetch()) {
  echo "<tr><td> $bookid </td> <td> $title </td><td> $author
</td></tr>";
I've copied the above code verbatim from the tutorial. Any help will be greatly appreciated.
Last edited by gdawg on Wed Jul 17, 2013 4:33 pm, edited 1 time in total.
Linux gdawg-Inspiron-530s 3.2.0-23-generic-pae #36-Ubuntu SMP Tue Apr 10 22:19:09 UTC 2012 i686 i686 i386 GNU/Linux
gdawg
 
Posts: 14
Joined: Fri Nov 18, 2011 2:55 pm
Location: New Mexico, USA

Postby Ram » Wed Jul 17, 2013 12:46 am

Quick glance, I can't see a closing tag for the php code.
Code: Select all
<?php
?>

lubuntu LXDE 13.10 running on AMD Phenom II*4; ASUS Crosshair III Formula MB; 4 GB Ram.....
User avatar
Ram
LXF regular
 
Posts: 1684
Joined: Thu Apr 07, 2005 9:44 pm
Location: Guisborough

Postby Ram » Wed Jul 17, 2013 1:52 pm

Your code appears to be missing the last 5 lines of code.

Code: Select all
}
echo "</table>";
?>
</body>
</html>

lubuntu LXDE 13.10 running on AMD Phenom II*4; ASUS Crosshair III Formula MB; 4 GB Ram.....
User avatar
Ram
LXF regular
 
Posts: 1684
Joined: Thu Apr 07, 2005 9:44 pm
Location: Guisborough

Postby gdawg » Wed Jul 17, 2013 4:28 pm

Thank you Ram. You're correct I failed to copy & paste the code correctly when I posted it.
Code: Select all
}
echo "</table>";
?>
</body>
</html>

It's there but I missed it.
Code: Select all
<!DOCTYPE html>
<html>
<head>
 <title>Library Book Search</title>
</head>
<body>
<h3>Book Search Results</h3><br>
<hr>
<?php
// Get data from form
$searchtitle = trim($_REQUEST['searchtitle']);
$searchauthor = trim($_REQUEST['searchauthor']);
if(!$searchtitle && !$searchauthor) {
 echo "You must specify either a title or an author";
 exit();
}
$searchtitle = addslashes($searchtitle);
$searchauthor = addslashes($searchauthor);
// Open the database
@ $db = new mysqli('localhost', 'root', '-t9m3s4e8d0*', 'library');
if($db->connect_error) {
   echo "could not connect:" .$db->connect_error;
   exit();
}
// Build the query. Users are allowed to search on title,
// author, or both
$query = "select * from books ";
if($searchtitle && !$searchauthor) { // Title search only
 $query = $query . " where title like '%" . $searchtitle . "%'";
}
if(!$searchtitle && $searchauthor) { // Author search only
 $query = $query . " where author like '%" . $searchauthor . "%'";
}
if($searchtitle && $searchauthor) { // Title and Author search
  $query = $query . "where title like '%" . $searchtitle . "%'
and
author like '%" . $searchauthor . "%'"; // unfinished
}
// Run the query using bound result parameters
$stmt = $db->prepare($query);
$stmt->bind_result($bookid, $title, $author, $onloan, $duedate, $borrowerid);
[b]echo $db->error;[/b]
$stmt->execute();
echo "<table border=1>";
while ($stmt->fetch()) {
  echo "<tr><td> $bookid </td> <td> $title </td><td> $author
</td></tr>";
}
echo "</table>";
?>
</body>
</html>

It's working now since I added

echo $db->error;
I don't know why. I don't see any errors being revealed. Thanks for your help.
Linux gdawg-Inspiron-530s 3.2.0-23-generic-pae #36-Ubuntu SMP Tue Apr 10 22:19:09 UTC 2012 i686 i686 i386 GNU/Linux
gdawg
 
Posts: 14
Joined: Fri Nov 18, 2011 2:55 pm
Location: New Mexico, USA


Return to Magazine and coverdiscs

Who is online

Users browsing this forum: No registered users and 4 guests