Blank screen with my php script

Hi Guys,

I’m going through ‘Build your own database driven web site’ by Kevin Yank, 4th edition.

I’m stuck in chapter 4 page 121

Ive copied everything, the only difference is Ive used my own web host to create a test site, but thought the only difference would be database name etc…

At any rate its not throwing up any errors at all, just a blank white screen.

PHP below.

<?php
$link = mysqli_connect('localhost', 'web81-johntrain', 'train567');
if (!$link)
{
$output = 'Unable to connect to the database server.';
include 'output.html.php';
exit();
}

if (!mysqli_set_charset($link, 'utf8'))
{
$output = 'unable to set database connection encoding.';
include 'output.html.php';
exit();
}

if (!mysqli_select_db($link, 'web81-johntrain'))
{
$output = 'Unable to locate the joke databse.';
include 'output.html.php';
exit()
}

$output = 'Database connection established.';
include 'output.html.php';
?>

Any help would be great thanks

Hi, try throwing some die() statements in there (put it as as the “else” in your if statements)

The page is just erroring out. It probably isn’t connecting to the database. Try using mysql_connect instead of mysqli :).

Many thanks,

Ive tried mysql instead of mysqli did nothing, I dont mind that its got an error but its just a blank screen… kind of need to know the error before fixing it.

Is there a problem with the code, im not sure i could go with that since its a sitepoint book (unless ive typed it wrong of course!)

Also I have no idea what die statements are (Im a beginner!)

Hi, your die() statements should be basically like this

mysql_connect(“localhost”, “admin”, “1admin”) or die(mysql_error());

Haven’t tested, but this is what your code should be with them included

<?php 
$link = mysqli_connect('localhost', 'web81-johntrain', 'train567') or die(mysql_error()); 
if (!$link) 
{ 
$output = 'Unable to connect to the database server.'; 
include 'output.html.php'; 
exit(); 
} 
else
{
die(mysql_error());
}

if (!mysqli_set_charset($link, 'utf8')) 
{ 
$output = 'unable to set database connection encoding.'; 
include 'output.html.php'; 
exit(); 
} 
else
{
die(mysql_error());
}


if (!mysqli_select_db($link, 'web81-johntrain')) 
{ 
$output = 'Unable to locate the joke databse.'; 
include 'output.html.php'; 
exit() 
} 
else
{
die(mysql_error());
}


$output = 'Database connection established.'; 
include 'output.html.php'; 
?>

Again thanks for looking at this, but alas still blank

Really stuck on this :frowning:

Try this:


<?php 

error_reporting(-1); 
ini_set('dispay_errors', TRUE);

#debug sttuff - saves re-typing 
defined('jj'); ? NULL : define('jj'. '<br />');
defined('js'); ? NULL : define('jj'. '&nbsp;&nbsp;');

echo jj, __LINE__, js, js, __FILE__; die;


$link = mysqli_connect('localhost', 'web81-johntrain', 'train567'); 
if (!$link) 
{ 
$output = 'Unable to connect to the database server.'; 
include 'output.html.php'; 
exit(); 
} 

if (!mysqli_set_charset($link, 'utf8')) 
{ 
$output = 'unable to set database connection encoding.'; 
include 'output.html.php'; 
exit(); 
} 

Still just a blank screen unfortunately, any more ideas? Could this be a problem with my hosting rather then the script?

I dont mind that the script isnt work just that its not showing anything at all!

Try this and if you still get a blank screen then another page is causing the error:



<?php 
echo  __FILE__; 
die;

# should display the filename and never ever get here :)



error_reporting(-1); 
ini_set('dispay_errors', TRUE);

#debug sttuff - saves re-typing 
defined('jj'); ? NULL : define('jj'. '<br />');
defined('js'); ? NULL : define('jj'. '&nbsp;&nbsp;');



Thanks, still blank.

Basically this is a static site with a folder to put this test script in, so not sure what would be clashing with it.

Could it be a hosting issue?

Is there anything working on this new test site you have created?

Try to get something to work then proceed from there in small steps :slight_smile:

Yeah Ive thrown up html files on there just fine

OK if the html files are working then:

  1. rename your “whatever-is-working.html” file to “whatever-is-working.php”
  2. include these lines at the top of the “whatever-is-working.php” file:
  3. run “whatever-is-working.php” in your browser

“whatever-is-working.php”



<?php
echo  __FILE__;
die;
?>

... Old Html stuff




what is contained in output.html.php?

Sometimes your host has error reporting turned off and if there is a syntax error in the page no matter where you put those die/exit statements you just get that nasty blank page. Try putting this at the top of you script after the opening PHP tag:


error_reporting(-1);

Hopefully that will display the error so you can debug it. Note this is a setting that can be tweaked in the PHP ini if you have that access.

Thanks everyone for your input, the answer is - I’m a doughnut! Typical beginner I guess…

I looked through my code again and my last ‘exit()’ didnt have a ‘;’ at the end, stuck it on, and lo and behold it works!

So my question is, why didnt that come up as an error? Whats happening to my script if I havent put the ‘;’ at the end?

Thanks everyone for your help

Read my last post. PHP was set to not show errors (this can be correct for some servers as you don’t want visitors seeing this information!) but for development it doesn’t make sense.

Read up on PHP configuration pertaining to the display of errors - here’s a page to get you started:

http://uk.php.net/manual/en/errorfunc.configuration.php

As Leonard said, your error reporting is turned off. It’s very important to turn on your error reporting (or view the error log on your server if you have access) when developing. It’ll make these simple problems become quick and easy. It is very painful to try and develop anything without the ability to access errors. =p