P粉7294365372023-08-25 12:07:07
You can also use PDO which I prefer. In fact, in your code example, you seem to be confusing PDO and Mysqli.
$db = new PDO($dsn, $user, $pass); $stmt = $db->prepare("INSERT INTO users (name, age) VALUES (?,?)"); $stmt->execute(array($name1, $age1)); $stmt->execute(array($name2, $age2));
Unlike mysqli, you don't need to call a separate bind function, although the functionality is available if you like/want/need to use it.
Another interesting thing about PDO is named placeholders, which may be less confusing in complex queries:
$db = new PDO($dsn, $user, $pass); $stmt = $db->prepare("INSERT INTO users (name, age) VALUES (:name,:age)"); $stmt->execute(array(':name' => $name1, ':age' => $age1)); $stmt->execute(array(':name' => $name2, ':age' => $age2));
P粉3480889952023-08-25 09:46:53
Frommysqli::prepare
Documentation:
Right now:
$name = 'one'; $age = 1; $stmt = $mysqli->prepare("INSERT INTO users (name, age) VALUES (?,?)"); // 绑定参数。我猜测是'string'和'integer',但请阅读文档。 $stmt->bind_param('si', $name, $age); // *现在*我们可以执行 $stmt->execute();