1. sql 语句执行原理
2. mysqli 预处理
php
header('Content-Type:text/html;charset=utf8');
$mysqli = new MySQLi('localhost','root','123456','test');
$sql = "insert into user1 (name,password,email,age) values(?,?,?,?)";
$mysqli_stmt = $mysqli->prepare($sql) or die($mysqli->error);
$name = '小红';
$password='xiaohong';
$email = 'aa@sina.com';
$age = '200';
$mysqli_stmt->bind_param('sssi',$name,$password,$email,$age);
$b = $mysqli_stmt->execute();
if(!$b){
die("操作失败" . $mysqli_stmt->error);
}else{
echo "操作成功";
}
$mysqli->close();
批量增加:
php
header('Content-Type:text/html;charset=utf8');
$mysqli = new MySQLi('localhost','root','123456','test');
$sql = "insert into user1 (name,password,email,age) values(?,?,?,?)";
$mysqli_stmt = $mysqli->prepare($sql) or die($mysqli->error);
$name = '小红';
$password='xiaohong';
$email = 'aa@sina.com';
$age = '200';
$mysqli_stmt->bind_param('sssi',$name,$password,$email,$age);
$b = $mysqli_stmt->execute();
$name = "老妖";
$password="laoyao";
$email="laoyao@qq.com";
$age='100';
$mysqli_stmt->bind_param('sssi',$name,$password,$email,$age);
$b = $mysqli_stmt->execute();
$name = "存储";
$password="cuenchu";
$email="cuenchu@qq.com";
$age='10';
$mysqli_stmt->bind_param('sssi',$name,$password,$email,$age);
$b = $mysqli_stmt->execute();
if(!$b){
die("操作失败" . $mysqli_stmt->error);
}else{
echo "操作成功";
}
$mysqli->close();
3.使用预处理进行查询
php
header('Content-Type:text/html;charset=utf8');
$mysqli = new MySQLi('localhost','root','123456','test');
if($mysqli->connect_error){
die($mysqli->connect_error);
}
$sql = "select id,name,email from user1 where id>?";
$mysqli_stmt = $mysqli->prepare($sql);
$id=5;
$mysqli_stmt->bind_param("i",$id);
$mysqli_stmt->bind_result($id,$name,$email);
$mysqli_stmt->execute();
while($mysqli_stmt->fetch()){
echo "
--$id--$name--$email";
}
$mysqli_stmt->free_result();
$mysqli_stmt->close();
$mysqli->close();
再次执行新的 sql 查询:
php
header('Content-Type:text/html;charset=utf8');
$mysqli = new MySQLi('localhost','root','123456','test');
if($mysqli->connect_error){
die($mysqli->connect_error);
}
$sql = "select id,name,email from user1 where id>?";
$mysqli_stmt = $mysqli->prepare($sql);
$id=5;
$mysqli_stmt->bind_param("i",$id);
$mysqli_stmt->bind_result($id,$name,$email);
$mysqli_stmt->execute();
while($mysqli_stmt->fetch()){
echo "
--$id--$name--$email";
}
echo "
*****绑定新的id号******";
$id=10;
$mysqli_stmt->bind_param("i",$id);
$mysqli_stmt->execute();
while($mysqli_stmt->fetch()){
echo "
--$id--$name--$email";
}
$mysqli_stmt->free_result();
$mysqli_stmt->close();
$mysqli->close();
预编译可以自动防止 sql 注入
4. 其他函数的使用
header('Content-Type:text/html;charset=utf8');
function showTable($table_name) {
$mysqli = new MySQLi('localhost','root','123456','test');
if($mysqli->connect_error){
die($mysqli->connect_error);
}
$sql = "desc user1";
$res = $mysqli->query($sql);
echo "共有 行" . $res->num_rows . "--列" . $res->field_count;
echo "
";
while($field = $res->fetch_field()){
echo "{$field->name} | ";
}
echo "";
while($row = $res->fetch_row()){
echo "";
foreach($row as $k=>$v ){
echo "$v | ";
}
echo "
";
}
echo "
";
$res->free();
$mysqli->close();
}
showTable("user1");
5. PDO 好处