2

I have a simple database that I am using to store the temperature of my pool and outside temperature every 10 to 30 minutes. The issue I am having is with the code that I have I can run the query locally in MySQL and it works just fine. But when I have the same query in PHP it gives me a blank page.

<?php
$dbhost = "localhost";
$dbuser = "xxxx";
$dbpass = "xxxxxxxxxx";
$dbname = "pool_db";

// Create connection
$conn = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname) or die("Error " .mysqli_error($conn));


$sql = "SELECT * FROM tempLog ORDER BY dateTime DESC";
$results = mysqli_query($conn, $sql) or die("Error " .mysqli_error());
?>
<!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>Untitled Document</title>
</head>

<body>
<?php
while($row = mysqli_fetch_array($results)); {
 ?>
<p><?php echo $row['poolTemp'];?></p>
<?php
}
?>
</body>
</html>
1
  • 1
    white page of death turn on error reporting and display Commented Apr 30, 2015 at 3:16

2 Answers 2

4

It's this line, the semi-colon:

while($row = mysqli_fetch_array($results)); {
                                          ^ end of statement character

It just kills it right there and will fail silently, in turn giving you a blank screen; remove it.

  • It is a valid character which will not throw an error. Therefore using error reporting is futile.

As in C or Perl, PHP requires instructions to be terminated with a semicolon at the end of each statement. The closing tag of a block of PHP code automatically implies a semicolon; you do not need to have a semicolon terminating the last line of a PHP block. The closing tag for the block will include the immediately trailing newline if one is present.

  • The keyword here being "terminated".

  • Also make sure you've in fact a row called "poolTemp" for $row['poolTemp'].

My own tests:

[blank screen] - with semi-colon

<?php 

$link = mysqli_connect('xxx','xxx','xxx', 'xxx'); 
if (!$link) { 
    die('Could not connect to MySQL: ' . mysqli_error()); 
} 
echo 'Connection OK'; 

echo "<hr>";

$sql = mysqli_query($link,"SELECT * FROM table");

while($row = mysqli_fetch_assoc($sql));{
                                   // ^ with semi-colon

echo $row['col1'] . " " . $row['col2'] . "<br>";

}

 mysqli_close($link);

?> 

[results successfully shown] - without semi-colon

<?php 

$link = mysqli_connect('xxx','xxx','xxx', 'xxx'); 
if (!$link) { 
    die('Could not connect to MySQL: ' . mysqli_error()); 
} 
echo 'Connection OK'; 

echo "<hr>";

$sql = mysqli_query($link,"SELECT * FROM table");

while($row = mysqli_fetch_assoc($sql)) {
                                   // ^ no semi-colon

echo $row['col1'] . " " . $row['col2'] . "<br>";

}

 mysqli_close($link);

?> 
Sign up to request clarification or add additional context in comments.

Comments

-1

Change mysqli_fetch_array to mysqli_fetch_row. Also for bonus points, do yourself a favor and echo the paragraph markup so you can avoid breaking up all of the code.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.