ggfan
ggfan

Reputation: 2492

Can I write prepared statements in procedural style?

The example from the PHP manual is using OOP. Is there a way to do it procedurally?

Upvotes: 14

Views: 14041

Answers (3)

mickmackusa
mickmackusa

Reputation: 47992

Procedural style syntax is a little more verbose and less preferred among most modern PHP developers. On top of that, PDO offers more sophisticated methods versus mysqli.

Regardless, yes, you can execute prepared statements with mysqli using procedural syntax.

Code: (PHPize.online Demo)

$price = '80.00';
$id = 3;

$sql = <<<SQL
SELECT *
FROM products
WHERE full_price = ?
  AND product_id = ?
SQL;

$stmt = mysqli_prepare($mysqli, $sql);
mysqli_stmt_bind_param($stmt, "si", $price, $id);
mysqli_stmt_execute($stmt);

foreach (mysqli_stmt_get_result($stmt) as $row) {
     var_dump($row);
}

Upvotes: 0

Lotus Notes
Lotus Notes

Reputation: 6363

Yes, you can. As far as I know PDO is completely object-oriented, but you may want to look into mysqli which allows both procedural and OO styles.

Procedural coders will find the basics almost identical. Where before you would used a function such as mysql_connect(), the new function is simply mysqli_connect(). Most of the old mysql_x functions have equivalent mysqli_x versions.

Source

Upvotes: 4

Amber
Amber

Reputation: 527053

MySQLi has procedural-style function calls that can handle prepared statements. (Some of the procedural-style functions are deprecated, however.)

http://us.php.net/manual/en/mysqli-stmt.prepare.php

Upvotes: 11

Related Questions