Using PHP/MYSQL book but get no output from PHP when trying to access the database

Tags: , ,



I’m currently working my way through “PHP and MySQL Web Development.” I’ve successfully created databases and been able to make tables and use the database. I’ve also successfully completed all the chapters on PHP and have had no problems with PHP not working up to this point. The goal of this page is return search results from a database. It’s a pretty simple thing to do but for some reason nothing is being output from the script to the page. I’m getting no errors or anything. It’s just blank with the title at the top. Can anyone please help me out with this? Thank you.

Here is the PHP code:

<html>
<head>
<title>Book-O-Rama Search Results</title>
</head>
<body>
<h1>Book-O-Rama Search Results</h1>
<?php
// create short variable names
$searchtype=$_POST['searchtype'];
$searchterm=trim($_POST['searchterm']);

 if (!$searchtype || !$searchterm) {
 echo 'You have not entered search details.  Please go back and try again.';
 exit;
 }

 if (!get_magic_quotes_gpc()){
 $searchtype = addslashes($searchtype);
 $searchterm = addslashes($searchterm);
 }

 @ $db = new mysqli('localhost', 'bookorama', 'bookorama123', 'books');

 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 = $result->num_rows;

 echo "<p>Number of books found: ".$num_results."</p>";

 for ($i=0; $i <$num_results; $i++) {
 $row = $result->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>

Answer

You’re supressing the errors from the line:

@ $db = new mysqli('localhost', 'bookorama', 'bookorama123', 'books');

Thats what the @ sign does, remove the @ sign and verify that the connection works properly, it might be that your script fails there.

You shouldn’t use that, it’s not considered good practice as far as I know.



Source: stackoverflow