tags:

views:

205

answers:

6
+4  Q: 

PHP Die question

Just a quick question. Say a call a method like so

mysql_pconnect("server","tator_w","password")
               or die("Unable to connect to SQL server");

Can I have the 'die' call a method rather then display a text message? If so, how?

+5  A: 

You would be better off using an if statement rather than relying on short-circuit evaluation if you want to do anything more complicated, e.g.:

if (!mysql_pconnect("server","tator_w","password")) {
    call_a_function();
    //some other stuff
    die(); //if you still want to die
}
Tom Haigh
would this work?mysql_pconnect("server","tator_w","password") or return false;
Steven1350
no, return is a language construct that you can't use in an expression. You could do if (!mysql_pconnect(blah)) { return false; }
Tom Haigh
Not sure why you'd want to do "or return false", since in that case you can just return the value of the first operand...
Peter
What Peter is saying is that mysql_pconnect returns false if the connection fails. So there's no need to return false in that case.
jared
yeah, i'm an idiot sometimes
Tom Haigh
although you might not actually want to return the resource, so you could do 'return (bool) mysql_pconnect();' or 'return !! mysql_pconnect();'
Tom Haigh
A: 

Well, not exactly, but you just do

if(!mysql_pconnect("server","tator_w","password")) {
    $some_obj->some_method();
    exit(1);
}
chaos
A: 

Why not just put in a function call that returns a string?


function myDieFunction()
{
     // do some stuff

     return("I died!");
}

die(myDieFunction());

Or you could try the register shutdown function

mgroves
A: 

Another (but not so nice) way:

mysql_pconnect("server","tator_w","password")
    or foo() & bar() & die("Unable to connect to SQL server");

Note the binary operator & instead of a boolean operator to have all the functions called.

Gumbo
Shame PHP doesn't have the comma operator for situations like that. But I suppose it'd just confuse 99% of PHP developers.
chaos
@chaos: Sure. But that’s just for the sake of completeness. I hope no one is ever using this.
Gumbo
+3  A: 

register_shutdown_function()

It lets you register a function that will be called when the system exits. Then you can simply die() or exit() without a parameter, which will call your method.

(you may also find set_error_handler() interesting, if slightly unrelated)

Jeremy Smyth
A: 

Not being able to connect to the database is probably a severe problem - I consider it a prime target for the use of exceptions.

If you are not able to connect to the database the problem probably needs to be handled delicately, and you probably want to log something about what went wrong, and where it went wrong to be able to make your code better to avoid the problem in the future.

Just a quick sketch up a way to use exceptions.

file view_cart.php

<?php
try
{
    require_once('bootstrap.php');
    require_once('cart.php');

    require('header.php');


    // Get the items in the cart from database
    $items = Cart::getItems();

    // Display them to the user
    foreach ($items as $item)
    {
        echo $item->name.', '$item->price.'<br />';
    }
}
catch (Exception $e)
{
    // Log the exception, it will contain useful info like
    // the call stack (functions run, paramaters sent to them etc)
    Log::LogException($e);

    // Tell the user something nice about what happened
    header('Location: technical_problem.html');
}

require('footer.php');

file bootstrap.php

<?php
$result = mysql_pconnect("server", "tator_w", "password");
if ($result === false)
{
    throw new Exception('Failed to connect to database');
}

// Select database
// Setup user session
// Etc
pcguru