A blank Page (revised)

I’ve had this problem before also and I posted the question but my account was on hold and that question got lost in millions of other questions before it could be answered so I am asking it again.

I am connecting a very simple page to mysqli database to fetch data from mysqli but the page is showing blank. Here is the code that I am using.

    <?php
	// 1. Create a database connection
	$dbhost = "localhost";
	$dbuser = "widget_cms";
	$dbpass = "Jaseemkhan@81";
	$dbname = "widget_corp";
	$connection = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);
	// Test if connection succeeded
	if (mysqli_connect_errno()){
		die("Database Connection failed: " . 
				mysqli_connect_error () . 
				" (" . mysqli_connect_errno() . " )"
			);
	}

 ?>

<?php include("../includes/functions.php");?>
<?php 
	// 2. Perform database query
	$query = "select * from subjects
			where visible = 1 
			order by position ASC";
	$result = mysqli_query($connection, $query);
	// Test if there was a query error
	if (!$result){
		die("Database query failed.");
	}
?>
<?php include("../includes/layouts/header.php");?>
<div id = "main">
	<div id="navigation">
		<ul>
			<?php 
				// 3. Use returned data (if any)
				while ($subject = mysqli_fetch_assoc($result)){
					// output data from each row
			?>
			<li><?php echo $subject ["menu_name"] . " (" .
			$subject["id"] . ")";?></li>
		}
		</ul>
	</div>
	<div id="page">
		<h2>Manage Content</h2>
	</div>
</div>
<?php 
	// 4. Release returned data
	mysqli_free_result($result);
?>
<?php include("../includes/layouts/footer.php");?>
<?php 
	// 5. Close the database connection
	mysqli_close($connection);
?>

The following are the most common reasons for a blank php page -

  1. Php parse/syntax errors (the code on the page doesn’t run at all.)
  2. Fatal php runtime errors (the code halts before producing output.)
  3. The php language is not installed or is not being invoked when/how the page gets requested (the raw php being output to the browser looks like it is just a html tag and nothing is rendered by the browser.)
  4. The page is not outputting anything at all, is conditionally outputting content and the conditional logic is false, or is outputting content/php non-fatal runtime errors and is performing a header() redirect with php’s output_buffering being turned on.

For items #1 and #2, you should ALWAYS have php’s error_reporting set to E_ALL and set display_errors to ON when learning, developing, and debugging code, and set display_errors to OFF and log_errors to ON when running code on a live/public server. These settings should be in the php.ini on your system so that you don’t need to remember to set/change them in your code, so that parse/syntax errors will be reported, and so that they can be changed at a single point. You may need to restart your web server to get any changes made to the php.ini to take effect and you need to confirm that the settings actually got changed by using a phpinfo() statement in a .php file, in case the php.ini that you changed is not the one that php is using.

For item #3, are you sure php is installed on the web server where you are executing this code, that you are using a URL, such as http://localhost/your_file.php, to request the file on the web server, and that the file has a .php extension? If the php code is not being executed on the server, it will be output to the browser. You may need to do a ‘view source’ in the browser to see the raw php code for a blank page.

For item #4, since the code has html that would produce visible content and does not contain a header() redirect, the problem is one of the above. However, while you are making changes to the php.ini for the error related settings, set output_buffing to OFF too.

I turned on the error reporting and now it says that there is a syntex error line number 56 which is the last line of code. I deleted that line that says close the database connection. Again the the syntex error showing the last line. I’ve checked it properly, I dont see any syntex error. Or maybe my eyes dont see it.

that’s only half the truth, the real message is longer.

php_error1

this mostly relies to a missing bracket. so you could go on deleting lines from the bottom.

I’ve checked it throughly, there are no missing brackets.

There’s one missing closing }. The reason you can see it, but php doesn’t, is because it is not inside php code, it’s inside some html markup. You have an excessive number of opening and closing php tags, that has created a ‘cannot see the forest for the trees’ problem. Stop needlessly switching into and out of php mode. If a section of code contains mostly php code, stay in php mode and just echo the small amount of html. Also, you can put php variables inside double quoted strings and have them replaced with their value, so the concatenation can also be removed.

Try this:

<?php

include("../includes/layouts/header.php");
include("../includes/functions.php");
	// 1. Create a database connection
	$dbhost = "localhost";
	$dbuser = "widget_cms";
	$dbpass = "Jaseemkhan@81";
	$dbname = "widget_corp";
	$connection = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);
	// Test if connection succeeded
	if (mysqli_connect_errno($connection)){
		die("Database Connection failed: " . 
				mysqli_connect_error () . 
				" (" . mysqli_connect_errno() . " )"
			);
	}
	// 2. Perform database query
	$query = "select * from subjects
			where visible = 1 
			order by position ASC";
	$result = mysqli_query($connection, $query);
	// Test if there was a query error
	if (!$result){
		die("Database query failed.");
	}
	else{
				// 3. Use returned data (if any)
				while ($subject = mysqli_fetch_assoc($result)){
					// output data from each row
			 echo $subject["menu_name"];
		}
	}
Sponsor our Newsletter | Privacy Policy | Terms of Service