Home > Backend Development > PHP Tutorial > How to Effectively Use Prepared Statements in MySQL with PHP?

How to Effectively Use Prepared Statements in MySQL with PHP?

Linda Hamilton
Release: 2025-01-02 16:14:39
Original
172 people have browsed it

How to Effectively Use Prepared Statements in MySQL with PHP?

Getting Started with Prepared Statements in MySQL

Prepared statements are an essential tool for writing secure and efficient SQL queries. In this article, we will explore how to use prepared statements with mysqli, the MySQLi extension in PHP.

Syntax Error: Non-Object Execution

The error you encounter, "Fatal error: Call to a member function execute() on a non-object," typically indicates that the $stmt variable is not properly initialized or an object. Here's how to correct it:

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

Ensure that you have a valid connection to the database before proceeding.

Binding Parameters

Prepared statements use parameter markers (e.g., ?) to represent input values. These values need to be bound to PHP variables before executing the statement. Here's an example:

$name = 'one';
$age = 1;
$stmt->bind_param('si', $name, $age);
Copy after login

In this example, we bind the name parameter as a string ('s') and the age parameter as an integer ('i').

Executing the Statement

Once the parameters are bound, you can execute the prepared statement:

$stmt->execute();
Copy after login

Handling Errors

Prepared statements provide better error handling than direct SQL queries. Use the mysqli_stmt::error method to retrieve error messages:

if ($stmt->error) {
    die("Error: " . $stmt->error);
}
Copy after login

Complete Example

Here's a complete example of inserting, selecting, and handling errors:

// Establish connection
$mysqli = new mysqli("localhost", "root", "root", "test");

// Prepare and bind parameters
$stmt = $mysqli->prepare("INSERT INTO users (name, age) VALUES (?,?)");
$stmt->bind_param('si', $name, $age);

// Insert multiple rows
$name = 'one';
$age = 1;
$stmt->execute();
$name = 'two';
$age = 2;
$stmt->execute();

// Prepare and execute select statement
$stmt = $mysqli->prepare("SELECT * FROM users");
$stmt->execute();

// Bind result
$result = $stmt->get_result();

// Process results
while ($row = $result->fetch_assoc()) {
    echo $row['name'] . ", " . $row['age'] . "
\n"; } // Handle errors if ($stmt->error) { die("Error: " . $stmt->error); }
Copy after login

By using prepared statements, you can prevent SQL injection attacks and write more robust and efficient SQL queries.

The above is the detailed content of How to Effectively Use Prepared Statements in MySQL with PHP?. 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