Home > Database > Mysql Tutorial > Why Am I Getting 'Fatal error: Call to a member function execute() on a non-object' When Using Mysqli Prepared Statements?

Why Am I Getting 'Fatal error: Call to a member function execute() on a non-object' When Using Mysqli Prepared Statements?

Linda Hamilton
Release: 2024-12-25 15:15:16
Original
896 people have browsed it

Why Am I Getting

Pitfalls in Using Mysqli Prepared Statements

In an attempt to utilize prepared statements, a developer encountered challenges and received the error "Fatal error: Call to a member function execute() on a non-object." This article delves into the root cause of this issue and provides a comprehensive guide on the proper usage of prepared statements in Mysqli.

Origin of the Error

The issue arises from the omission of parameter binding before executing the prepared statement. Mysqli requires the binding of parameters to application variables via the mysqli_stmt_bind_param() function before executing the statement.

Implementing Prepared Statements

1. Parameter Binding:

$name = 'one';
$age  = 1;

$stmt = $mysqli->prepare("INSERT INTO users (name, age) VALUES (?,?)");

// bind parameters. Refer to documentation for appropriate data types (e.g., 'si' for string and integer).
$stmt->bind_param('si', $name, $age);
Copy after login

2. Statement Execution:

$stmt->execute();
Copy after login

3. Subsequent Parameter Binding and Execution:

For inserting multiple rows with different values, the parameter binding step must be repeated before each execution.

Example:

$name = 'two';
$age  = 2;

// Update bound parameters
$stmt->bind_param('si', $name, $age);

// Execute with different values
$stmt->execute();
Copy after login

4. Complete Example:

$mysqli = new mysqli("localhost", "root", "root", "test");
if ($mysqli->connect_errno) {
    echo "Failed to connect to MySQL: " . $mysqli->connect_error;
}

$stmt = $mysqli->prepare("INSERT INTO users (name, age) VALUES (?,?)");
try {
    // Insert one row
    $name = 'one';
    $age  = 1;
    $stmt->bind_param('si', $name, $age);
    $stmt->execute();

    // Insert another row with different values
    $name = 'two';
    $age  = 2;
    $stmt->bind_param('si', $name, $age);
    $stmt->execute();
} catch (Exception $e) {
    echo "Error: " . $e->getMessage();
}
Copy after login

The above is the detailed content of Why Am I Getting 'Fatal error: Call to a member function execute() on a non-object' When Using Mysqli Prepared Statements?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template