為什麼我的 PDO 程式碼中出現「在非物件上呼叫成員函數準備()」錯誤?

Susan Sarandon
發布: 2024-11-14 16:04:02
原創
807 人瀏覽過

Why Am I Getting a

pdo - Call to a member function prepare() on a non-object

When attempting to validate user input using PDO, an error is encountered indicating that the prepare() method is being called on a non-object. This error occurs specifically on line 42 of the provided code.

To resolve this issue, inspect the code to ensure that the $pdo object is properly defined and accessible within the repetirDados() function. The error suggests that $pdo is not available in the current scope.

Confirm if $pdo is being passed as an argument to the function or if it is defined as a global variable. If neither of these scenarios is true, you can define $pdo within the global namespace and use the global keyword within the function to access it:

global $pdo;
登入後複製

Alternatively, you can pass $pdo as an argument to the repetirDados() function, ensuring that it is available within the function's scope.

Equivalent to mysql_num_rows

The equivalent of mysql_num_rows in PDO is rowCount(). It returns the number of rows affected by the most recent query execution. In this case, you can use rowCount() to check if any rows were returned from the query:

if ($stmt->rowCount() == 0) {
    // No results found
} else {
    // Results found
}
登入後複製

以上是為什麼我的 PDO 程式碼中出現「在非物件上呼叫成員函數準備()」錯誤?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
作者最新文章
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板