PDO Call to Member Function Prepare on a Non-Object: Causes and Solutions
When attempting to use the prepare() method of the PDO object, an error can occur: "Call to a member function prepare() on a non-object." This error indicates that the $pdo object is not properly initialized within the function causing the issue.
In the provided code snippet, the repetirDados() function doesn't declare the $pdo object or pass it as a parameter. To resolve this error, you can either:
PDO Equivalent of MySQL Functions
You also inquired about the PDO equivalent of MySQL function mysql_num_rows. PDO provides an alternative method called rowCount() which serves the same purpose. It returns the number of affected rows in the result set after executing a query.
In your code, you can use $stmt->rowCount() instead of $results == 0 to check if any rows were returned by the query.
The above is the detailed content of Why Am I Getting 'Call to a Member Function Prepare() on a Non-Object' in My PDO Code?. For more information, please follow other related articles on the PHP Chinese website!