Marc_CAT Posted December 26, 2009 Share Posted December 26, 2009 Hi! Today is the first day i try to program using PHP. I have downloaded the last XAMPP version, and created a simple database for testing. Now i wanted to write a php piece of code to retrieve the data I have placed in the database, so after looking different tutorial pages, i have this: <!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>Test</title> </head> <body> <?php $username="root"; $password="aaaa"; $database="marc_book"; $con = mysql_connect("localhost",$username,$password); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db($database); $result = mysql_query("SELECT * FROM book"); $num = mysql_numrows($result); mysql_close(); $i=0; while ($i < $num) { $isbn=mysql_result($result,$i,"isbn"); $title=mysql_result($result,$i,"title"); $pages=mysql_result($result,$i,"page_count"); echo "$isbn $title $pages"; $i++; } ?> </body> </html> When I run it, I get nothing. I hope a more skilled php-programmer will be able to detect what's wrong. Thank you!! Marc Quote Link to comment https://forums.phpfreaks.com/topic/186375-beginner-question-php-code-does-not-retrieve-data-from-mysql/ Share on other sites More sharing options...
ignace Posted December 26, 2009 Share Posted December 26, 2009 It's mysql_num_rows and not mysql_numrows add these lines to your script: error_reporting(E_ALL); ini_set('display_errors', 1); That is after the <?php Quote Link to comment https://forums.phpfreaks.com/topic/186375-beginner-question-php-code-does-not-retrieve-data-from-mysql/#findComment-984207 Share on other sites More sharing options...
Marc_CAT Posted December 26, 2009 Author Share Posted December 26, 2009 Hi ignace. Thanks for the suggestion. I have added the underscore in mysql_num_rows, but I continue getting the same blank page in the browser. The database has been created using phpMyAdmin. The path to the database (in my code is set to "localhost") is correct? Or should I add anything else? Thanks again! Quote Link to comment https://forums.phpfreaks.com/topic/186375-beginner-question-php-code-does-not-retrieve-data-from-mysql/#findComment-984235 Share on other sites More sharing options...
PFMaBiSmAd Posted December 26, 2009 Share Posted December 26, 2009 mysql_numrows is an old depreciated alias for mysql_num_rows and won't prevent your code from working. When you do a "view source" in your browser of the blank page, what do you get? Did you add the two lines of code that ignace suggested that would show all php runtime errors? Quote Link to comment https://forums.phpfreaks.com/topic/186375-beginner-question-php-code-does-not-retrieve-data-from-mysql/#findComment-984236 Share on other sites More sharing options...
Marc_CAT Posted December 26, 2009 Author Share Posted December 26, 2009 Hello! When you do a "view source" in your browser of the blank page, what do you get? I get the HTML code, which I believe is the same from the original html file: <!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>Test</title> </head> <body> <?php error_reporting(E_ALL); ini_set('display_errors', 1); $username="root"; $password="aaaa"; $database="marc_book"; $con = mysql_connect("localhost",$username,$password); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db($database); $result = mysql_query("SELECT * FROM book"); $num = mysql_num_rows($result); mysql_close(); $i=0; while ($i < $num) { $isbn=mysql_result($result,$i,"isbn"); $title=mysql_result($result,$i,"title"); $pages=mysql_result($result,$i,"page_count"); echo "$isbn $title $pages"; $i++; } ?> </body> </html> Did you add the two lines of code that ignace suggested that would show all php runtime errors? Yes, i did! Thanks PFMaBiSmAd! Quote Link to comment https://forums.phpfreaks.com/topic/186375-beginner-question-php-code-does-not-retrieve-data-from-mysql/#findComment-984239 Share on other sites More sharing options...
PFMaBiSmAd Posted December 26, 2009 Share Posted December 26, 2009 If the raw php code is present in the "view source" then either php is not functioning on your web server or the file extension is not .php or you browsed directly to the file instead of browsing to it through a URL. What URL are you using in your browser? It should be something like http://localhost/yourfile.php Quote Link to comment https://forums.phpfreaks.com/topic/186375-beginner-question-php-code-does-not-retrieve-data-from-mysql/#findComment-984241 Share on other sites More sharing options...
Marc_CAT Posted December 26, 2009 Author Share Posted December 26, 2009 If the raw php code is present in the "view source" then either php is not functioning on your web server or the file extension is not .php or you browsed directly to the file instead of browsing to it through a URL. What URL are you using in your browser? It should be something like http://localhost/yourfile.php Yes, the error was there. The extension of the file was .html, instead of .php. Now it works properly. Thanks a lot, it would have been frustrating not succeeding in an example as easy as this one. Marc Quote Link to comment https://forums.phpfreaks.com/topic/186375-beginner-question-php-code-does-not-retrieve-data-from-mysql/#findComment-984242 Share on other sites More sharing options...
bruceleejr Posted December 26, 2009 Share Posted December 26, 2009 I asked the same question ... here is my source code so you can take a look and take anything you need: <? $fn = $_POST["fn"]; $ln = $_POST['ln']; $ff = $_POST['ff']; $fh = $_POST['fh']; $yb = $_POST['yb']; $mar = $_POST['mar']; $dbc = mysqli_connect('mysql5.000webhost.com', 'a3393387_c2use', '******', 'a3393387_c2db') or die('Cant connect to MySQL servies~!!'); $query = "INSERT INTO testing (fn,ln, " . " ff, fh, yb, mar) " . " VALUES ('$fn', '$ln', '$ff', '$fh', '$yb', '$mar')"; $result = mysqli_query($dbc, $query) or die('error querying database'); mysql_connect('mysql5.000webhost.com', 'a3393387_c2use', '******') or die('error conecint'); echo "Connected to mysql~! <br />"; mysql_select_db('a3393387_c2db'); echo "Connected to database~! <br />"; echo "<b>Displaying details...</b> <br /><br />"; $query1 = "SELECT * FROM testing"; $result1 = mysql_query($query1) or die('cant select tables'); while($row = mysql_fetch_array($result1)) { echo $row['fn'] ."-". $row['ln'] ."-". $row['ff'] ."-". $row['fh'] ."-". $row['yb'] ."-". $row['mar']; echo "<br />"; } mysqli_close($dbc); ?> <a href="custom2.html">go back to steve's info form</a> http://testo.comoj.com/custom2.php Quote Link to comment https://forums.phpfreaks.com/topic/186375-beginner-question-php-code-does-not-retrieve-data-from-mysql/#findComment-984293 Share on other sites More sharing options...
Marc_CAT Posted December 27, 2009 Author Share Posted December 27, 2009 I asked the same question ... here is my source code so you can take a look and take anything you need: <? $fn = $_POST["fn"]; $ln = $_POST['ln']; $ff = $_POST['ff']; $fh = $_POST['fh']; $yb = $_POST['yb']; $mar = $_POST['mar']; $dbc = mysqli_connect('mysql5.000webhost.com', 'a3393387_c2use', '******', 'a3393387_c2db') or die('Cant connect to MySQL servies~!!'); $query = "INSERT INTO testing (fn,ln, " . " ff, fh, yb, mar) " . " VALUES ('$fn', '$ln', '$ff', '$fh', '$yb', '$mar')"; $result = mysqli_query($dbc, $query) or die('error querying database'); mysql_connect('mysql5.000webhost.com', 'a3393387_c2use', '******') or die('error conecint'); echo "Connected to mysql~! <br />"; mysql_select_db('a3393387_c2db'); echo "Connected to database~! <br />"; echo "<b>Displaying details...</b> <br /><br />"; $query1 = "SELECT * FROM testing"; $result1 = mysql_query($query1) or die('cant select tables'); while($row = mysql_fetch_array($result1)) { echo $row['fn'] ."-". $row['ln'] ."-". $row['ff'] ."-". $row['fh'] ."-". $row['yb'] ."-". $row['mar']; echo "<br />"; } mysqli_close($dbc); ?> <a href="custom2.html">go back to steve's info form</a> http://testo.comoj.com/custom2.php Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/186375-beginner-question-php-code-does-not-retrieve-data-from-mysql/#findComment-984590 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.