問題描述
我知道此代碼在我擁有的另一個網站上有效,但今天無法正常運行.我收到三個警告:
I know that this code works on another site I've got but it's not playing ball today. I get three warnings:
警告:mysqli_stmt_bind_param() 期望參數 1 為 mysqli_stmt,布爾值在/homepages/14/d248783986/htdocs/subdomains/clients.bionic-comms.co.uk/httpdocs/carefree/process.php 第 33 行
Warning: mysqli_stmt_bind_param() expects parameter 1 to be mysqli_stmt, boolean given in /homepages/14/d248783986/htdocs/subdomains/clients.bionic-comms.co.uk/httpdocs/carefree/process.php on line 33
警告:mysqli_execute() 期望參數 1 為 mysqli_stmt,布爾值在/homepages/14/d248783986/htdocs/subdomains/clients.bionic-comms.co.uk/httpdocs/carefree/process.php 第 34 行
Warning: mysqli_execute() expects parameter 1 to be mysqli_stmt, boolean given in /homepages/14/d248783986/htdocs/subdomains/clients.bionic-comms.co.uk/httpdocs/carefree/process.php on line 34
警告:mysqli_stmt_affected_rows() 期望參數 1 為 mysqli_stmt,布爾值在/homepages/14/d248783986/htdocs/subdomains/clients.bionic-comms.co.uk/httpdocs/carefree/process.php 第 35 行
Warning: mysqli_stmt_affected_rows() expects parameter 1 to be mysqli_stmt, boolean given in /homepages/14/d248783986/htdocs/subdomains/clients.bionic-comms.co.uk/httpdocs/carefree/process.php on line 35
有人能幫我解決這個問題嗎?
Can someone help me figure this out?
如果有幫助,我必須使用 htaccess 升級到 PHP5.
I am having to use htaccess to upgrade to PHP5 if this helps.
$connection = mysqli_connect($hostname, $username, $password, $dbname);
if (!$connection) {
die('Connect Error: ' . mysqli_connect_error());
}
$query = "INSERT INTO entries (name, dob, school, postcode, date) VALUES (?,?,?,?,?)";
$stmt1 = mysqli_prepare($connection, $query);
mysqli_stmt_bind_param($stmt1, 'sssss',$name,$dob,$school,$postcode,$date);
mysqli_execute($stmt1);
if(mysqli_stmt_affected_rows($stmt1) != 1)
die("issues");
mysqli_stmt_close($stmt1);
return "new";
編輯
經過一些調查發現,prepare 語句與 mysql4 不起作用.我創建了一個新的 mysql5 數據庫,但是當我嘗試連接時出現此錯誤:
After some investigation it transpires that the prepare statement doesn't play ball with mysql4. I have created a new mysql5 database but I now get this error when I try to connect:
Warning: mysqli_connect() [function.mysqli-connect]: (HY000/2005): Unknown MySQL server host 'localhost:/tmp/mysql5.sock' (1)
Warning: mysqli_connect() [function.mysqli-connect]: (HY000/2005): Unknown MySQL server host 'localhost:/tmp/mysql5.sock' (1)
有人知道為什么會這樣嗎?
Does anyone have any idea as to why this is happening?
推薦答案
添加更多錯誤處理.
當連接失敗時,mysqli_connect_error() 可以告訴你更多細節.
當準備語句失敗時,mysqli_error() 有關于錯誤的更多信息.
當語句執行失敗時,詢問mysqli_stmt_error().等等等等...
任何時候 mysqli 模塊的函數/方法返回 false 以指示錯誤,a) 處理該錯誤和 b) 決定繼續是否有意義.例如.連接失敗時繼續進行數據庫操作是沒有意義的.但是當只有一次插入失敗時繼續插入數據可能是有意義的(可能有意義,可能沒有).
Add more error handling.
When the connection fails, mysqli_connect_error() can tell you more details.
When preparing the statement fails mysqli_error() has more infos about the error.
When executing the statement fails, ask mysqli_stmt_error(). And so on and on...
Any time a function/method of the mysqli module returns false to indicate an error, a) handle that error and b) decide whether it makes sense or not to continue. E.g. it doesn't make sense to continue with database operations when the connection failed. But it may make sense to continue inserting data when just one insertion failed (may make sense, may not).
為了測試,你可以使用這樣的東西:
For testing you can use something like this:
$connection = mysqli_connect(...);
if ( !$connection ) {
die( 'connect error: '.mysqli_connect_error() );
}
$query = "INSERT INTO entries (name, dob, school, postcode, date) VALUES (?,?,?,?,?)";
$stmt1 = mysqli_prepare($connection, $query);
if ( !$stmt1 ) {
die('mysqli error: '.mysqli_error($connection);
}
mysqli_stmt_bind_param($stmt1, 'sssss',$name,$dob,$school,$postcode,$date);
if ( !mysqli_execute($stmt1) ) {
die( 'stmt error: '.mysqli_stmt_error($stmt1) );
}
...
對于真正的生產環境,這種方法很健談,而且很容易死掉;-)
For a real production environment this approach is to talkative and dies to easily ;-)
這篇關于Mysqli 拋出“警告:mysqli_stmt_bind_param() 期望參數 1 為 mysqli_stmt,給出布爾值";的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!