Alex
Alex

Reputation: 751

Basic PHP problem, code won't run! (no errors just blank)

Ok, so I'm trying to make some simple code to display news articles from a MySQL server but all I get is a completely blank middle part of the page where the news articles are supposed to be. Here is the code:

<?
  $query = "SELECT ID, header, content FROM news ORDER BY ID DESC";
  $result = mysql_query($query);

  while($row = mysql_fetch_array($result))
  {
    echo "<div class=\"newsItem\">";
    echo "<h2>" . $row['header'] . "</h2>";
    echo "<p>" . $row['content'] . "</p>";
    echo "</div>";
  }
  ?>

The problem seems to be with the while loop. If I write echo "WTF"; outside the loop it will show but if i write it inside it wont show. I'm not really good at PHP so I'm puzzled. ID is INT and Primary Key, header is VARCHAR(255) and content is TEXT. Any Ideas? Also the connect scrips works cuz I dont get error messages when it dies.

Upvotes: 0

Views: 248

Answers (4)

Stacey Richards
Stacey Richards

Reputation: 6606

You may have a database rights issue or your query may have an error but the error isn't being displayed.

If you don't have display_errors turned on in your php.ini, you should take a look in the web server error log file to see if an error is being logged when you connect to the MySql database.

You could also try:

ini_set('display_errors', 1);

At the top of your script which will force any database connection or query errors to be displayed in the resulting web page.

NOTE: This is a feature to support your development and should never be used on production systems.

Upvotes: 0

genesis
genesis

Reputation: 50966

  • There aren't any news in news table
  • One or more columns are missing
  • Table news does not exists

in your case, try to replace your 2nd line with

  $query = "SELECT ID, content FROM news ORDER BY ID DESC";

Upvotes: 1

Karoly Horvath
Karoly Horvath

Reputation: 96258

You either have no records in your news table or displaying warnings isn't enabled (slap)

Upvotes: 2

Naftali
Naftali

Reputation: 146302

Try adding an error catch:

  $query = "SELECT ID, header, content FROM news ORDER BY ID DESC";
  $result = mysql_query($query) or die(mysql_error());

OR you have no results. so add somthing for that:

if(mysql_num_row($result) > 0){
  while($row = mysql_fetch_array($result))
  {
    echo "<div class=\"newsItem\">";
    echo "<h2>" . $row['header'] . "</h2>";
    echo "<p>" . $row['content'] . "</p>";
    echo "</div>";
  }
}
else {echo 'no results';}

Upvotes: 5

Related Questions