Forum Moderators: coopster
I've created a MySQL connection - as a PHP include for security. So the connection itself is separate from the page it is called from. There's only one simple use for this database connection so far- to pull a specific value for a hotel price from a table and insert it in into a hotel detail page. I'd like to create custom error messages for those (hopefully rare) occassions when the database cannot be accessed for some reason.
The way it is now, if the connection doesn't work, there's a string of error messages like this:
Parse error: parse error in /myconnection/include/path on line 7Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in /mysite/directory/page.php on line 413
Warning: mysql_fetch_row(): supplied argument is not a valid MySQL result resource in /mysite/directory/page.php on line 414
Warning: mysql_close(): supplied argument is not a valid MySQL-Link resource in /mysite/directory/page.php on line 416
I could change the connection to die if the connection fails - BUT, I don't want PHP to stop parsing the whole page just because the connection failed. I'd like a simple error message like "Temporarily unavailable' to print where the price value should go, and the rest of the page be rendered as normal. (Gosh, I hope that's clear!)
Here's how the connection script is called, with a sample SQL query:
<?php
include('MyConnection.php');
$query = "SELECT doubleratemidweek FROM price WHERE DatabaseRefNo = 112780";
$result = mysql_query($query, $dbConn);
$price = mysql_fetch_row($result);
echo $price[0];
mysql_close($dbConn);
?> Here's my connection script
<?php
$host = 'mymysqlserver';
$dbUser = 'myuser';
$dbPass = 'mypassword';
$dbName = 'mydbname';
if (!$dbConn = mysql_connect($host, $dbUser, $dbPass)) {
die('Connection Temporarily Unavailable');
}
if (!mysql_select_db($dbName, $dbConn)) {
die('Temporarily Unavailable');
}
?>
Is there a way to have the non-databased part of the page rendered normally, and a simple, user-friendly error message printed if the connection fails?
that way you set a var true or false that you can test to see if the connection was successful.
should work
the top part could be something like this
<?php
include('MyConnection.php');
if ($conn) {
$query = "SELECT doubleratemidweek FROM price WHERE DatabaseRefNo = 112780";
$result = mysql_query($query, $dbConn);
$price = mysql_fetch_row($result);
echo $price[0];
mysql_close($dbConn);
} else {
echo $dberr;
}
?>
Warning: mysql_fetch_row(): supplied argument is not a valid MySQL result resource in /path/to/my/test/page.php on line 415
Line 415 is the string
$price = mysql_fetch_row($result);
There's probably something easy I'm missing, but I don't see it.
To recap, this is my current script:
<?php
include('MyConnection.php');
if ($dbConn) {
$query = "SELECT doubleratemidweek FROM price WHERE DatabaseRefNo = 112780";
$result = mysql_query($query, $dbConn);
$price = mysql_fetch_row($result);
echo $price[0];
mysql_close($dbConn);
} else {
echo $dberr;
}
?>
and this is the connection script that I'm calling with the include:
<?php
$dbConn = true;
$host = 'myMySQLhost';
$dbUser = 'myUser';
$dbPass = 'myPassword';
$dbName = 'mydb';
if (!$dbConn = mysql_connect($host, $dbUser, $dbPass)) $conn = false;
if (!mysql_select_db($dbName, $dbConn)) $conn = false;
if (!$conn) $dberr = "Temporarily Unavailable";
?>
<?php
include('MyConnection.php');
if (!$dberr) {
$query = "SELECT doubleratemidweek FROM price WHERE DatabaseRefNo = 112780";
$result = mysql_query($query, $dbConn);
$price = mysql_fetch_row($result);
echo $price[0];
mysql_close($dbConn);
} else {
echo $dberr;
}
?>
I should have spotted that. Your suggestion is a big improvement. The only trouble is that the $dberr is not printing. There's just a blank space instead of the value of $dberr.
This must be an error in how I have declared that variable, right?
In my connection script I've got:
if (!$dbConn) $dberr = "Temporarily Unavailable";
and on my page I've got:
echo $dberr;
That looks right to me, but I'm new to this. What am I missing?
It should be:
<?php
$conn = true;
$host = 'myMySQLhost';
...
...
...
if (!$conn) $dberr = "Temporarily Unavailable";
?>
$dbConn is the actual connection, whereas $conn is just the true/false on whether the connection has been made successfully.