2013-03-06 24 views
-2

我想要添加html內容來代替mysql錯誤,所以而不是失敗時,我得到一個只有黑色背景和文本'錯誤'的mysql錯誤。添加html內容到mysql死亡錯誤?

mysql_query($query) or die ('error'); 

我想讓它顯示這個網站的內容:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"> 
<head> 
    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8" /> 
    <title></title> 

    <link rel="stylesheet" type="text/css" href="css/style.css" media="all" /> 


</head> 
<body> 

<div id="container"> 
    <h1>REGISTRATION WAS UNSUCCESFULL</h1><br/><br/><br/><br/> 
    <div class="textarea"> 
     <h3>Whoop! Good News we've successfully receieved your registration. So now what we've done, is sent you an email. In this email you will be guided through the final stage of account set-up.<br/><br/>Keep an eye on your inbox and follow the instructions. We hope to see you real soon ;-)</h3> 
    </div> 
     <div class="man_reg"><img src="../assets/img/help_support/man.png" alt="" width="210" height="214" /></div></div> 
    <div id="progress_bar"> 
     <div id="progress"></div> 
     <div id="progress_text">Registration Completed</div> 
    </div> 

    <style> 
    .textarea{ 
     padding-left:55px; 
     padding-right:55px; 
     text-align:center; 
    } 
    .man_reg{ 
     margin-top:54px; 
     margin-left:450px; 
    } 

    </style>  
</body> 
</html> 

我豈不只是插入了MySQL的括號內的HTML內容或死亡的錯誤?

像這樣:

mysql_query($query) or die ('HTML CONTENT HERE!'); 
+0

你爲什麼還在使用'mysql_'的功能呢?它們已被棄用,存在更好的替代方案。 – DCoder 2013-03-06 10:19:34

回答

1

只是存儲在一個變量HTML內容,並使用該變量在die

$error = "<html>your html content</html>"; 

mysql_query($query) or die ($error); 
1

您不必使用or die()mysql_query回報false上發生故障,使工作與:

$result = mysql_query(...); 

if (!$result) { 

    // do anything necessary to display a page here, 
    // maybe include() an external file 

} else { 

    ... 

} 
1

更好的解決辦法是有一個錯誤頁面的內容,只是將用戶重定向到它用下面的代碼:

$res = mysql_query($query); 
if (!$res) { 
    header('Location: http://yourdomain.com/error.html'); 
    die(); // just to end up the execution 
} 
1

您基本上只是將內容放在括號內,但更優雅的方式是使用單獨的error.html模板和include()。這樣,你的代碼不會被大量的標記污染。

的error.html(簡體):

<html><head></head><body><?php echo $error; ?></body></html> 

PHP代碼:

$result = mysql_query(....); 

if (!$result) 
{ 
    $error = mysql_error(); 
    include "error.html"; 
    die(); 
} 

請注意,這是不好的風格顯示在生產環境中的錯誤。您可能希望在開發時顯示確切的錯誤消息,並且在項目公開時會出現通用的「發生錯誤」頁面。

0

你可以使用的功能也爲這個

function dieerr() { 
echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"> 
<head> 
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8" /> 
<title></title> 

<link rel="stylesheet" type="text/css" href="css/style.css" media="all" /> 


</head> 
<body> 

<div id="container"> 
<h1>REGISTRATION WAS UNSUCCESFULL</h1><br/><br/><br/><br/> 
<div class="textarea"> 
    <h3>Whoop! Good News we\'ve successfully receieved your registration. So now what we\'ve done, is sent you an email. In this email you will be guided through the final stage of account set-up.<br/><br/>Keep an eye on your inbox and follow the instructions. We hope to see you real soon ;-)</h3> 
</div> 
    <div class="man_reg"><img src="../assets/img/help_support/man.png" alt="" width="210" height="214" /></div></div> 
<div id="progress_bar"> 
    <div id="progress"></div> 
    <div id="progress_text">Registration Completed</div> 
</div> 

<style> 
.textarea{ 
    padding-left:55px; 
    padding-right:55px; 
    text-align:center; 
} 
.man_reg{ 
    margin-top:54px; 
    margin-left:450px; 
} 

</style>  
</body> 
</html>'; 
exit; 
} 

如果你有這樣的則使用這樣的:

mysql_query($query) or dieerr();