Forum Moderators: coopster

Message Too Old, No Replies

Custom error message for MySQL connection

print error message and NOT die

         

dbr1066

1:05 am on Feb 12, 2004 (gmt 0)

10+ Year Member



Hi all,

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 7

Warning: 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


-------
In other words, if the connection fails, there's a parse error message, then all the other variables called by the queries each give error messages.

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?

jatar_k

1:12 am on Feb 12, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



<?php
$conn = true;
$host = 'mymysqlserver';
$dbUser = 'myuser';
$dbPass = 'mypassword';
$dbName = 'mydbname';
if (!$dbConn = mysql_connect($host, $dbUser, $dbPass)) $conn = false;
if (!mysql_select_db($dbName, $dbConn)) $conn = false;
if (!$conn) $dberr = "Temporarily Unavailable";
?>

that way you set a var true or false that you can test to see if the connection was successful.

should work

dbr1066

2:28 am on Feb 12, 2004 (gmt 0)

10+ Year Member



Thanks, jatar_k,

I think I see the logic in your code. Presumably I have to then insert the $dberr variable somewhere in my page code? (sorry if this is "obvious", but at my stage of learning PHP not too much is obvious at all!<grin>)

jatar_k

2:42 am on Feb 12, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



no problem

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;
}
?>

dbr1066

3:21 am on Feb 12, 2004 (gmt 0)

10+ Year Member



Thanks again. I tried it as you suggested, and to see how this script would behave if a connection couldn't connect, I intentionally altered the dbName value. It does not print the custom error message, instead it gives this error message:

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";
?>

scumm_bar2

9:37 am on Feb 12, 2004 (gmt 0)

10+ Year Member



By using if ($dbConn) { you're telling it to test for a connection again. Use if (!$dberr) { and it will use the (custom) error of the first connection test.

<?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;
}
?>

dbr1066

4:03 pm on Feb 12, 2004 (gmt 0)

10+ Year Member



Thanks scumm_bar2,

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?

scumm_bar2

4:30 pm on Feb 12, 2004 (gmt 0)

10+ Year Member



Yep, I think the one I gave you is right for that part, but you might be getting confused between $dbConn and $conn, which are two totally different things.

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.

dbr1066

5:21 pm on Feb 12, 2004 (gmt 0)

10+ Year Member



Wow! You were right on (and right about my confusion). I can't tell you how much I appreciate everyone's help. I love this forum!

Hopefully someday I'll learn enough to help out other struggling PHP newbies :-)