MjM8082 Posted July 24, 2012 Share Posted July 24, 2012 I am fairly new to php and I'm learning mysql. I'm building a blog for practice, and I managed to get the blog post to insert into the database but now I want it to display on my index.php page but I can't seem to get it to work and I dont know what I am doing wrong.... here is my code include "connect_to_mysql.php"; $result = mysql_query("SELECT * FROM posts"); while($row = mysql_fetch_array($result)) { echo $row['title'] . " " . $row['content']; echo "<br />"; } Quote Link to comment https://forums.phpfreaks.com/topic/266155-posting-data-on-page-from-database/ Share on other sites More sharing options...
MrXkill Posted July 24, 2012 Share Posted July 24, 2012 I am fairly new to php and I'm learning mysql. I'm building a blog for practice, and I managed to get the blog post to insert into the database but now I want it to display on my index.php page but I can't seem to get it to work and I dont know what I am doing wrong.... here is my code include "connect_to_mysql.php"; $result = mysql_query("SELECT * FROM posts"); while($row = mysql_fetch_array($result)) { echo $row['title'] . " " . $row['content']; echo "<br />"; } Instead of: while($row = mysql_fetch_array($result)) Try this: while($row = myhsql_fetch_assoc($result)) Quote Link to comment https://forums.phpfreaks.com/topic/266155-posting-data-on-page-from-database/#findComment-1363928 Share on other sites More sharing options...
MjM8082 Posted July 24, 2012 Author Share Posted July 24, 2012 nope still nothing appears on the screen Quote Link to comment https://forums.phpfreaks.com/topic/266155-posting-data-on-page-from-database/#findComment-1363933 Share on other sites More sharing options...
peipst9lker Posted July 24, 2012 Share Posted July 24, 2012 Have u looked over your database with PHPMyAdmin or something like that - are there really entries in the database? If yes place this line above your first echo (inside the while) var_dump($row); Execute the script and check the output. Quote Link to comment https://forums.phpfreaks.com/topic/266155-posting-data-on-page-from-database/#findComment-1363934 Share on other sites More sharing options...
Donald. Posted July 24, 2012 Share Posted July 24, 2012 Change the include "connect_to_mysql.php"; to require_once "connect_to_mysql.php" so the page ensures you get the file before executing anything else. It's usually a good practice, in my opinion, as the script will not work if you can't get that file. <? require_once ("connect_to_mysql.php"); $result = mysql_query("SELECT * FROM posts") or die("SELECT Error: ".mysql_error()); //This will tell you if there is an error with the query. while($row = mysql_fetch_array($result)) { echo $row['title'] . " " . $row['content']; echo "<br />"; } ?> Also what are you encountering a blank page, any errors? Quote Link to comment https://forums.phpfreaks.com/topic/266155-posting-data-on-page-from-database/#findComment-1363937 Share on other sites More sharing options...
peipst9lker Posted July 24, 2012 Share Posted July 24, 2012 Donald you still need to use mysql_fetch_assoc() in this case. Quote Link to comment https://forums.phpfreaks.com/topic/266155-posting-data-on-page-from-database/#findComment-1363940 Share on other sites More sharing options...
PFMaBiSmAd Posted July 24, 2012 Share Posted July 24, 2012 A) 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 php will help you by reporting and displaying all the errors it detects? Stop and start your web server to get any changes made to your master php.ini to take affect and confirm that the settings actually changed by using a phpinfo statement in case the php.ini that php is using is not the one that you changed. B) What does a 'view source' of the blank page in your browser show? C) mysql_fetch_array returns both the associative and numerical array and won't change the way the code operates. D) Use the or die("SELECT Error: ".mysql_error()); on the end of your mysql_query() statement that Donald. suggested to check if your query is producing an error. Quote Link to comment https://forums.phpfreaks.com/topic/266155-posting-data-on-page-from-database/#findComment-1363991 Share on other sites More sharing options...
MjM8082 Posted July 25, 2012 Author Share Posted July 25, 2012 Alright here is my updated code that I ran... require_once ("connect_to_mysql.php"); $result = mysql_query("SELECT * FROM posts") or die("SELECT Error: ".mysql_error()); //This will tell you if there is an error with the query. while($row = mysql_fetch_array($result)) { echo $row['title'] . " " . $row['content']; echo "<br />"; } After running that I received this on the page.... "; } ?> that was it.. Not sure what the error can be, everything looks right? Quote Link to comment https://forums.phpfreaks.com/topic/266155-posting-data-on-page-from-database/#findComment-1364167 Share on other sites More sharing options...
peipst9lker Posted July 25, 2012 Share Posted July 25, 2012 That's weird - can you post the whole script? If it's too big put in on a nopaste or something. Quote Link to comment https://forums.phpfreaks.com/topic/266155-posting-data-on-page-from-database/#findComment-1364169 Share on other sites More sharing options...
MjM8082 Posted July 25, 2012 Author Share Posted July 25, 2012 <?php include "connect_to_mysql.php"; $sql = mysql_query("SELECT * FROM posts WHERE title='$title' AND content='$content'"); $result = mysql_query ($query); while ($row = mysql_fetch_array($result)) { $title = stripslashes($row['title']); $display_block .= "<p><strong></strong> $title </p>"; } ?> <!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>Johns's Blog</title> <script type="text/javascript" src="jquery.js"></script> <style type="text/css"> @import url(css.css); </style> <script type="text/javascript" src="js.js"></script> <link rel="shortcut icon" href="images/maddynavi.png" /> </head> <body> <body> <div id="logo"><center><img src="images/banner.png"/></center></div> <div id="wrapper"> <h1>John's Blog</h1> <ul id="nav"> <li><a href="index.html">blog</a></li> <li><a href="about.html">about me</a></li> <li><a href="pictures.html">pictures</a></li> <li><a href="videos.html">videos</a></li> </ul> <div id="content"> <h2><img src="images/blogblog.png"/></h2> </br> <p><?php print 'title' ?></p> </div> <div id="foot"><a href="admin_login.php">Admin</a></div> </div> </body> </html> This is all the code on the index page. Quote Link to comment https://forums.phpfreaks.com/topic/266155-posting-data-on-page-from-database/#findComment-1364170 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.