2015-01-15 52 views
0

我是網絡開發新手,所以可能有些事情我做錯了。PHP - 查詢mysql時出錯處理

我正在使用webmatrix進行開發,並使用webmatrix提供的StarterSite示例進行播放。

在php文件(header.php)中有一個使用mysqli擴展名的查詢。我已將tablename更改爲某個不存在的表以模擬錯誤情況。問題是,經過以下聲明 -

$statement->execute(); 

腳本停止。 我在執行後插入了一個echo語句,並且該回顯字符串未顯示在網頁上。但是,當我更正表名時,執行後的回顯字符串顯示在網頁上。所以我認爲腳本在表名錯誤時停止執行。我有兩個問題。如何停止腳本停止執行?其次如何確定腳本已經停止執行某些特定的語句?

對於問題的第二部分,我檢查了IISExpress文件夾中的日誌文件和tracelog文件。沒有提到任何錯誤,可能是因爲錯誤發生在MYSQL中。但是,在我的MYSQL文件夾中沒有日誌文件,所以不知道如何檢查mysql日誌。

如果我錯過了任何東西,請告訴我。

問候, 圖莎爾

+0

讀'mysqli' PHP手冊部分,可能會有一些有用的信息。 – 2015-01-15 17:40:26

+0

爲什麼使用IIS作爲網絡服務器時使用PHP和MySQL不是相關的;只是好奇?過去,使用PHP和MySQL的IIS在交通繁忙時遇到了問題。 – unixmiah 2015-01-15 18:26:26

+0

難道你不能爲你的問題想出一個獨特的(或者至少有一半有趣的)標題嗎?也請只寫...每個問題...,呃,問題! – 2015-01-15 19:14:55

回答

0

你應該閱讀有關mysqli的錯誤處理。

基本的錯誤處理例如OOP:

if ($mysqli->connect_errno) { 
    printf("Connect failed: %s\n", $mysqli->connect_error); 
    exit(); 
} 

程序:

if (mysqli_connect_errno()) { 
    printf("Connect failed: %s\n", mysqli_connect_error()); 
    exit(); 
} 
0

這取決於你正在登錄的。在錯誤日誌中,您可以定義正在記錄的內容。我想你可以在php.ini中控制錯誤的嚴格模式,它會自動將錯誤輸入到access_log或error_log或apache日誌文件中。

訣竅是在mysqli查詢和db連接的每一步中使用$ mysqli-> error,以確保您得到正確的錯誤消息,詳細說明是調試,改進代碼還是正確執行代碼。

以下是在查詢數據庫時使用$ mysqli->錯誤的示例。

$result = $mysqli->query($query); 
if (!$result and $mysqliDebug) { 
// the query failed and debugging is enabled 
echo "<p>There was an error in query: $query</p>"; 
echo $mysqli->error; //additional error 
} 

您還可以使用一種方法,可以定義MySQL錯誤在康涅狄格州分貝

// define a variable to switch on/off error messages 
$mysqliDebug = true; 
// connect to your database 
// if you use a single database, passing it will simplify your queries 
$mysqli = @new mysqli('localhost', 'myuser', 'mypassword', 'mydatabase'); 
// mysqli->connect_errno will return zero if successful 
if ($mysqli->connect_errno) { 
echo '<p>There was an error connecting to the database!</p>'; 
if ($mysqliDebug) { 
// mysqli->connect_error returns the latest error message, 
// hopefully clarifying the problem 
// NOTE: supported as of PHP 5.2.9 
echo $mysqli->connect_error; 
} 
// since there is no database connection your queries will fail, 
// quit processing 
die(); 
} 

@ref是真實的:https://www.daniweb.com/web-development/php/code/434480/using-phpmysqli-with-error-checking