Build your own database driven website using php & mysql - 4th Ed, Kevin Yank

Hi everyone,

I’m working my way through the book “Build your own database driven website using php & mysql - 4th Edition”.

In chapter 4, when testing for a connection to the database server AND when testing for a connection to the jokes database, the variable used was $error and so therefore there was a separate error.html.php template page. But when testing for setting the database connection encoding, the variable used was $output and so there was a separate output.html.php template page.

My question is why are there two separate variables and template files. I mean can’t you just use the $error variable and related template file since all three are testing for something and therefore if the test evaluates to false then it’s an error right?

The following is the code used in the book:

$link = mysqli_connect('localhost', 'root', 'password');
if (!$link)
	$error = 'Unable to connect to the database server.';
	include 'error.html.php';

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

if (!mysqli_select_db($link, 'ijdb'))
	$error = 'Unable to locate the joke database.';
	include 'error.html.php';

Appreciate any comments/advice.

I’m guessing that the author considers an encoding error to be less troublesome than the others. In those, there will be no output. With the encoding there will be output but some characters may appear wonky. Thus the user will see results but is warned about the (potential) problems and will know why if they see “funny looking” characters.

Thanks for the reply,

If I understand you correctly, you were saying that there is no output for the $error variables. These errors are being output in the file called error.html.php so both $error and $output are being written to the browser so I’m not sure if that’s the problem.

Sorry, I wasn’t very clear. The “html.php” files will output yes, what I meant is they will not have any results from the database in the output when “connect” or “select” fail. But when “charset” fails, there will still be results, just that they may not display 100% correctly.

I don’t understand how you can say that if “charset” fails then there will still be results. When charset fails, it includes a file called output.html.php which contains the following code.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>


<p><?php echo $output;?></p>

Therefore the only thing that’s output to the browser is the value of $output which is ‘Unable to set database connection encoding’ and not any further results.

So I still don’t understand why the author is using the two variables, $output and $error.

Well, if that’s all it does I can see your point. Not much sense to that IMHO
Maybe later on in the book there are changes so that the page will show something more?
In any case, there are database results such as they are, whether or not the book uses them you could if you wanted to.

Ok thanks for the advice