문제
빠른 질문입니다. 그렇게하는 방법을 전화하십시오
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
}
다른 팁
시스템이 종료 될 때 호출 될 함수를 등록 할 수 있습니다. 그런 다음 간단하게 할 수 있습니다 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");
이진 연산자에게 주목하십시오 &
부울 연산자 대신 모든 기능을 호출합니다.
데이터베이스에 연결할 수없는 것은 아마도 심각한 문제 일 것입니다. 예외 사용의 주요 대상이라고 생각합니다.
데이터베이스에 연결할 수 없다면 문제를 섬세하게 처리해야 할 것입니다. 아마도 문제가 발생한 것에 대해 무언가를 기록하고, 코드가 미래.
예외를 사용하는 방법을 빠르게 스케치하십시오.
파일보기 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
제휴하지 않습니다 StackOverflow