ScrewLooseSalad Posted February 4, 2013 Share Posted February 4, 2013 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! Quote Link to comment https://forums.phpfreaks.com/topic/274018-cannot-read-mysql-database/ Share on other sites More sharing options...
PFMaBiSmAd Posted February 4, 2013 Share Posted February 4, 2013 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? Quote Link to comment https://forums.phpfreaks.com/topic/274018-cannot-read-mysql-database/#findComment-1410052 Share on other sites More sharing options...
ScrewLooseSalad Posted February 4, 2013 Author Share Posted February 4, 2013 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 Quote Link to comment https://forums.phpfreaks.com/topic/274018-cannot-read-mysql-database/#findComment-1410054 Share on other sites More sharing options...
ScrewLooseSalad Posted February 4, 2013 Author Share Posted February 4, 2013 I just checked and the "<?php" line comes immediately after the h1, so the next thing I did was check php was working correctly, and it is... Quote Link to comment https://forums.phpfreaks.com/topic/274018-cannot-read-mysql-database/#findComment-1410056 Share on other sites More sharing options...
PFMaBiSmAd Posted February 4, 2013 Share Posted February 4, 2013 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.) Quote Link to comment https://forums.phpfreaks.com/topic/274018-cannot-read-mysql-database/#findComment-1410063 Share on other sites More sharing options...
ScrewLooseSalad Posted February 4, 2013 Author Share Posted February 4, 2013 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> Quote Link to comment https://forums.phpfreaks.com/topic/274018-cannot-read-mysql-database/#findComment-1410065 Share on other sites More sharing options...
PFMaBiSmAd Posted February 4, 2013 Share Posted February 4, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/274018-cannot-read-mysql-database/#findComment-1410068 Share on other sites More sharing options...
Christian F. Posted February 4, 2013 Share Posted February 4, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/274018-cannot-read-mysql-database/#findComment-1410069 Share on other sites More sharing options...
ScrewLooseSalad Posted February 4, 2013 Author Share Posted February 4, 2013 I see, I'll remove that line now, and post the results in a bit Quote Link to comment https://forums.phpfreaks.com/topic/274018-cannot-read-mysql-database/#findComment-1410070 Share on other sites More sharing options...
ScrewLooseSalad Posted February 4, 2013 Author Share Posted February 4, 2013 Not only is this about 13 years out of date, and thus wrong, but it's also insecure. I'm working from a book, so I suspected I'd get a problem like that Quote Link to comment https://forums.phpfreaks.com/topic/274018-cannot-read-mysql-database/#findComment-1410071 Share on other sites More sharing options...
PFMaBiSmAd Posted February 4, 2013 Share Posted February 4, 2013 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.) Quote Link to comment https://forums.phpfreaks.com/topic/274018-cannot-read-mysql-database/#findComment-1410073 Share on other sites More sharing options...
ScrewLooseSalad Posted February 4, 2013 Author Share Posted February 4, 2013 how perplexing, I'm getting a "Error: Could not connect to database. Please try again later.". I got this before I put in the correct log in settings, but it stopped when I corrected them; I'm not sure if this is removing the "@" or something else I've done trying to figure it out... Quote Link to comment https://forums.phpfreaks.com/topic/274018-cannot-read-mysql-database/#findComment-1410075 Share on other sites More sharing options...
PFMaBiSmAd Posted February 4, 2013 Share Posted February 4, 2013 If you echo mysqli_connect_error(), it will tell you why the connection failed. Quote Link to comment https://forums.phpfreaks.com/topic/274018-cannot-read-mysql-database/#findComment-1410076 Share on other sites More sharing options...
ScrewLooseSalad Posted February 5, 2013 Author Share Posted February 5, 2013 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... Quote Link to comment https://forums.phpfreaks.com/topic/274018-cannot-read-mysql-database/#findComment-1410239 Share on other sites More sharing options...
Christian F. Posted February 5, 2013 Share Posted February 5, 2013 I would strongly consider putting that book away, and finding myself a more up to date (and correct one). You will find a lot of good book suggestions in this thread, just don't go too far back into the history. Quote Link to comment https://forums.phpfreaks.com/topic/274018-cannot-read-mysql-database/#findComment-1410240 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.