首页 > 代码库 > PHP 操作 mysql 预处理
PHP 操作 mysql 预处理
由于本地 PHP 版本过底,没办法进行完整的测试
$host = ‘localhost‘;
$user = ‘root‘;
$password = ‘‘;
$dbname = ‘cmstop‘;
$mysqli = new mysqli($host, $user, $password, $dbname) or exit(‘无法连接到mysql:‘.$mysqli->connect_error);
$sql = "SELECT CURRENT_USER(); SELECT * FROM `db_setting` LIMIT 5;";
if ($mysqli->multi_query($sql)){//多条查询
do{
if($result = $mysqli->store_result()){//如果还有还结果集,取当前的一个结果集返回
while($row = $result->fetch_row()){//取一条记录
echo $row[0];
}
$result->free();//当此结果集中无记录时,释放
}
if($mysqli->more_results()){//如果还有一个结果集,则输出分隔线
echo "<hr />";
}
}while($mysqli->next_result());
}
//预处理
$sql = "SELECT `app`,`var` FROM `db_setting` WHERE `app`=? LIMIT 5";
$app = ‘article‘;
$stmt = $mysqli->stmt_init();
if($stmt->prepare($sql)) {
$stmt->bind_param("s", $app);
$stmt->execute();//执行查询
$stmt->bind_result($field1, $field2); //生成两个字段的结果集
while ($stmt->fetch()) {
echo ‘<br/>‘, $field1,‘ => ‘, $field2;
}
echo‘<hr/>‘;
$app = ‘guestbook‘;
$stmt->execute();//执行查询
$result = $stmt->get_result(); //生成两个字段的结果集
while ($row = $result->fetch_array(MYSQLI_NUM)){
echo $row[0],‘ => ‘,$row[1];
}
$stmt->close();
}
$mysqli->close();
PHP 操作 mysql 预处理