0

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

4
Contributors
6
Replies
26
Views
3 Years
Discussion Span
Last Post by tpunt
0

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.

0

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

Edited by JorgeM

0

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

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?

0

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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.