Jump to content

cannot read MySQL database


ScrewLooseSalad

Recommended Posts

I recently got to grips with PHP, and I have used MySQL on occasion as well, I am now trying to interface them for the first time; I have written up a database for tracking stock at my business and I want to view my tables in a web browser using PHP, however nothing I have written has worked, and all the code I have found and modified from the internet isn't producing any results either.

 

I have changed sample codes from the internet and in my books to use my MySQL username, password and tables, but I keep getting a blank page returned... I wonder if the code I have is out of date or something, I can't figure it out...

 

Any MySQL boffins available to explain how they would achieve this goal? Thanks!

Link to comment
Share on other sites

I keep getting a blank page returned...

 

You have to troubleshoot what your code is doing, before you can fix the problem.

 

For that specific symptom, what does a 'view source' of the blank page in your browser show? Do you have php's error_reporting set to E_ALL and display_errors set to ON in your master php.ini on your development system so that all the php detected errors will be reported and displayed? Are you sure the database statements are running without producing any database errors (do you have error checking logic in your code to test if the statements are failing)? Are you sure your database query is matching any rows?

Link to comment
Share on other sites

You have to troubleshoot what your code is doing, before you can fix the problem.

 

For that specific symptom, what does a 'view source' of the blank page in your browser show?

 

I clicked 'view source' on the page in my browser and found the following:

<html>
<head>
 <title>Search Results</title>
</head>
<body>
<h1>Search Results</h1>

 

That was it, before now I tried adding in 'echo' text at the stages of the search code, and all those echos appeared in the browser fine

Link to comment
Share on other sites

You need to troubleshoot why your php code is not outputting the expected results. For any one here to possibly help with the dozen different things our code could be doing, you would need to post the code that reproduces the problem (less any database connection information.)

Link to comment
Share on other sites

SEARCH

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

 

 

RESULTS

<html>
<head>
 <title>Search Results</title>
</head>
<body>
<h1>Book 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', 'stockcheck', 'evoque', 'v');
 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>

Link to comment
Share on other sites

Because you are not getting the - "<p>Number of books found:" displayed on the page, your code is probably not executing to that point.

 

Best guess is that the mysqli extension is not enabled. However, you are using the @ error suppressor on the line that would have produced an error message alerting you to that effect.

 

You should never use the @ error suppressor in any code, ever. On a development system, you want to see all php detected errors. On a live server, you want to log all php detected errors.

 

Remove the @ and I previously asked about your error_reporting/display_errors settings.

Link to comment
Share on other sites

I suspect that the issue lies here:

  $query = "select * from books where ".$searchtype." like '%".$searchterm."%'";
  $result = $db->query($query);
  $num_results = $result->num_rows;

 

The reason for this is that this is the only part where you're not handling errors, and thus doesn't explicitly echo out something in case of errors. If you'd turned on error reporting, as mentioned by PFM above, you would probably have seen an error about a method used on a non-object.

You need to check if $result is false first, and if it is show an error message stating that the query failed. Also, print out the completed query along with the error message from MySQL, at least on the development system.

Once you've done that, figuring out what the error is, and not to fix it, should be trivial. ;)

 

I would also like to mention this bit:

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

Not only is this about 13 years out of date, and thus wrong, but it's also insecure. What you should have been doing is to remove the slashes if magic_quotes were enabled. Then, when creating the query, you should be using MySQLI::real_escape_string () to protect yourself against SQL injections.

Also, remove the removeslashes () when echoing out the content. htmlspecialchars () will protect your code against HTML injection attacks (such as XSS, etc). Removing the stripslashes () prevent the possibility of the string to become corrupted, by the erroneous removal of slashes or rather anything that looks like slashes for the rs function.

Link to comment
Share on other sites

Since your php code is apparently running, you can set the error_reporting/display_errors settings using statements in your code. The reason for asking/suggesting putting the settings into your master php.ini on your development systems is so that fatal parse errors in your main file will be reported and displayed and you don't need to remember to put the settings into your code for development and remove them on a live server (you don't want to display php errors on a live server.)

Link to comment
Share on other sites

thanks for your help, I'm onto the next problem now :S

 

trying to call 'MDB2.php' stops all my code from running, perhaps its not installed, my book seems to assume the reader has it, and I can't find any other reference to it elsewhere; I've found the PEAR MDB2 website, but they want you to use a package manager, it seems a little over complicated for a php file for logging into MySQL...

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.