Mengapa Saya Mendapat Ralat 'Panggilan ke fungsi ahli prepare() pada bukan objek' dalam Kod PDO Saya?

Susan Sarandon
Lepaskan: 2024-11-14 16:04:02
asal
807 orang telah melayarinya

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;
Salin selepas log masuk

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
}
Salin selepas log masuk

Atas ialah kandungan terperinci Mengapa Saya Mendapat Ralat 'Panggilan ke fungsi ahli prepare() pada bukan objek' dalam Kod PDO Saya?. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Artikel terbaru oleh pengarang
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan