I'm trying to create a simple blog and for some reason , it doesnt retrieve the dummy content from the mysql database.
here is my entire code

<?php  
$db_host = "host"; 
// Place the username for the MySQL database here 
$db_username = "root";  
// Place the password for the MySQL database here 
$db_pass = "";  
// Place the name for the MySQL database here 
$db_name = "someblog"; 


$myConnection = mysqli_connect("$db_host","$db_username","$db_pass", "$db_name") or die ("could not connect to mysql");  

?> 
<html>   
    <head>
        <title> Test Blog</title>
    </head>
<body>
<?
$sqlCommand = "SELECT title, content, 'date modified ' FROM  blog1tables ORDER BY id DESC"; 
$query = mysqli_query($myConnection, $sqlCommand) or die (mysqli_error()); 
while ($row = mysqli_fetch_array($query)) { 
   $title = $row["title"];
$content = $row["content"];
$datemodified = ["date modified"];
}
mysqli_free_result($query); 
 ?>
 <table border = '4'>
  <tr><td <? echo $title;?> </td></tr>
  <tr><td colspan="4"> <? echo $content; ?> </td></tr>
  <tr><td> <? echo $datemodified; ?> </td></tr>

 </table>


</body>

</html>

help

Recommended Answers

All 6 Replies

Change:

$sqlCommand = "SELECT title, content, 'date modified ' FROM  blog1tables ORDER BY id DESC";

To:

$sqlCommand = "SELECT title, content, `date modified` FROM blog1tables ORDER BY id DESC";

Note the backticks instead of the single quotes.

You need to build your table rows and cells within the while loop. At the moment, your building your table outside of the loop.

Also note that one of your td tags is not closed properly. Line 30

I'm still not getting any output
<?php

$db_host = "localhost"; 
// Place the username for the MySQL database here 
$db_username = "root";  
// Place the password for the MySQL database here 
$db_pass = "";  
// Place the name for the MySQL database here 
$db_name = "someblog"; 


$myConnection = mysqli_connect("$db_host","$db_username","$db_pass", "$db_name");      
?> 
<html>   
    <head>
        <title> Test Blog</title>
    </head>
<body>
<?
$sqlCommand = "SELECT title, content, `date modified ` FROM  blog1tables WHERE  name='title' ,name = 'content' ORDER BY id ASC "; 
$query = mysqli_query($myConnection, $sqlCommand) or die (mysqli_error()); 
while ($row = mysqli_fetch_array($query)) { 
   $title = $row["title"];
$content = $row["content"];
$datemodified = ["date modified"];

mysqli_free_result($query); 
 ?>
 <table border = '4'>
  <tr><td> <? echo $title;?> </td></tr>
  <tr><td colspan="4"> <? echo $content; ?> </td></tr>
  <tr><td> <? echo $datemodified; ?> </td></tr>

 </table>
 <?
 }
 ?>

</body>

</html>

See the query, there's a space that shouldn't be there.

Regarding building the table, you wouldnt want to include the <table> tags within the loop since you only want one table.

Also, wouldnt you want mysqli_free_result() to be executed at the end of the process, not within your loop?

Just as a further addition to the improvements said above, you may want to use either the mysqli_fetch_assoc() function, or specify the second argument to the mysqli_fetch_array() function as MYSQLI_ASSOC. The reason being is that currently the result being set fetched is returning both an associative and numerically indexed array (due to the default argument being MYSQLI_BOTH). It's an unecessary waste of resources given that you're only accessing the array via the the column names.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.