开发者

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

开发者 https://www.devze.com 2023-03-24 04:31 出处:网络
Ok, so I\'m trying to make some simple code to display news articles from a MySQL server but all I get i开发者_Python百科s a completely blank middle part of the page where the news articles are suppos

Ok, so I'm trying to make some simple code to display news articles from a MySQL server but all I get i开发者_Python百科s 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.


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';}


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


  • 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";


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号