為什麼我在 PDO 中收到「在非物件上呼叫成員函數準備()」?

Patricia Arquette
發布: 2024-11-13 14:07:02
原創
473 人瀏覽過

Why Am I Getting

Call to Member Function prepare() on a Non-Object in PDO

This question revolves around an error encountered while using the PDO extension for PHP. The error suggests that the prepare() method was invoked on a non-object.

Possible Cause

The primary cause of this error lies in the undefined $pdo variable. It is essential to ensure that $pdo is properly defined or passed as an argument to the function where the prepare() method is being employed.

Alternative Solution

Alternatively, you can include global $pdo; at the beginning of the function to access the $pdo variable from the global scope. However, this approach is considered less desirable as it doesn't promote code modularity.

Equivalent to mysql_num_rows

Since PHP Data Objects (PDO) is a more advanced database abstraction layer than the now-deprecated MySQLi extension, it doesn't provide a direct equivalent to mysql_num_rows. However, you can obtain the number of rows affected by a query or fetched by a statement using rowCount().

For instance, you can modify your code to retrieve the number of affected rows:

$ok = $stmt->execute();
$rowCount = $stmt->rowCount();
登入後複製

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

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