我正在将我的mysql转换为OO mysqli。
所以,我不确定语法,如果我需要重复下面的代码。
我有一个像这样的php页面:
<?php
...
$mysqli = new mysqli($hostname, $user, $pass, $bd);
//first a select:
$stmt = $mysqli->prepare("SELECT user FROM cad WHERE user = ?");
$stmt->bind_param('s', $user);
$stmt->execute();
$result = $stmt->get_result();
$stmt->close();
$num = $result->num_rows;
if($num > 0){...}
//an update
$stmt = $mysqli->prepare("UPDATE cadastro SET `user` = ?");
$stmt->bind_param('s', $user);
$stmt->execute();
$stmt->close();
//an insert
$stmt = $mysqli->prepare("INSERT INTO cad (user) VALUES (?)");
$stmt->bind_param('s', $user);
$stmt->execute();
$stmt->close();
//second select
$stmt = $mysqli->prepare("SELECT user FROM cad WHERE user = ? and id=0");
$stmt->bind_param('s', $user);
$stmt->execute();
$result = $stmt->get_result();
$stmt->close();
$num = $result->num_rows;
if ($num==0){...do something...}
...
?>
我的问题是,是不是?结构是对的吗? 准备 捆绑 执行 得到结果 靠近...
准备 捆绑 执行 靠近...
...
或者我可以更经济或以最佳方式做到这一点?