Jump to content

Recommended Posts

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 />";
  } 

Link to comment
https://forums.phpfreaks.com/topic/266155-posting-data-on-page-from-database/
Share on other sites

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))

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.

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?

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.

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?

 

 

<?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.

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.