-2

我想添加 html 内容来代替 mysql 错误,所以当我得到一个只有黑色背景和文本“错误”的 mysql 错误时,而不是失败。

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

我希望它显示这个 html 内容:

<!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!');
4

5 回答 5

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 {

    ...

}
于 2013-03-06T10:19:20.470 回答
1

您基本上只是将内容放在括号内,但更优雅的方式是拥有一个单独的error.html模板并对其进行include()ing。这样,您的代码就不会被大量标记污染。

error.html(简体):

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

PHP代码:

$result = mysql_query(....);

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

请注意,在生产环境中显示错误是一种不好的风格。您可能希望在开发时显示确切的错误消息,并在项目公开时显示通用的“发生错误”页面。

于 2013-03-06T10:21:40.163 回答
1

更好的解决方案是创建一个包含该内容的错误页面,然后使用以下代码将用户重定向到该页面:

$res = mysql_query($query);
if (!$res) {
    header('Location: http://yourdomain.com/error.html');
    die(); // just to end up the execution
}
于 2013-03-06T10:20:26.020 回答
1

只需将您的 html 内容存储在一个变量中并使用该变量die

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

mysql_query($query) or die ($error);
于 2013-03-06T10:17:47.877 回答
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();
于 2013-03-06T10:22:30.093 回答