質問

Pdflibは簡単に利用できるapiを提供い。というメソッドの呼び出しのように

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

いただきたいのですが、可能で、"死メソッドの呼び出しというよりも表示テキストメッセージ?どうすれば良いですか。

役に立ちましたか?

解決

あなたが例えば、より複雑な何かを、行いたい場合は、

あなたはif文を使用してではなく、短絡評価に頼ったほうが良いでしょう:ます。

if (!mysql_pconnect("server","tator_w","password")) {
    call_a_function();
    //some other stuff
    die(); //if you still want to die
}

他のヒント

register_shutdown_function()する

それはあなたのシステムが終了したときに呼び出される関数を登録することができます。そして、あなたは単にあなたのメソッドを呼び出しますパラメータなしdie()またはexit()することができます。

(あなたも見つけることがはset_error_handler()の興味深いです、わずかに無関係の場合)

まあ、ない正確に、しかし、あなたはただ行う

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

なぜ、単なる文字列を返す関数呼び出しに入れていない?


function myDieFunction()
{
     // do some stuff

     return("I died!");
}

die(myDieFunction());

それとも、

別の(しかしとても素敵ではない)方法:

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

と呼ばれるすべての機能を持っている代わりにブール演算子のバイナリ演算子&

できないのでは、データベースへの接続には厳しい問題と考えるのでゲの使用は例外をスローしました。

が間に合わない場合は、その理は、データベースへの接続に問題のあのニーズを取り扱う繊細に、だいたいログインについて何かを行なってい、でっきるコードによる問題を回避するために。

Pdflibは簡単に利用できるapiを提供スケッチを使用方法と例外をスローしました。

ファイル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');

ファイル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
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top