PHP mysqli 预备语句

1.获取结果集

  $conn = mysqli_connect($host,$user,$password,$db);
  $result = $conn->query("select * from alfas");

  while($row = $result->fetch_row())
  {
     print $row[0] .' '.$row[1]. "<br>\n";
  }

 

 2.预备语句

 

 

 

 

  $conn = mysqli_connect($host,$user,$password,$db);
$conn
->query("create table alfas "."(year int(10), model varchar(50), accel real)"); //用?做占位符 $stmt = $conn->prepare("insert into alfas values(?,?,?)"); //绑定输入变量 $stmt->bind_param("isd",$year,$model,$accel); //赋值到绑定的输入变量 $year = 2001; $model = '156 2.0 selespeed'; $accel = 8.6; //执行预备语句 $result = $stmt->execute(); //赋值到绑定的输入变量 $year = 2002; $model = '156 2.4 selespeed'; $accel = 8.7; //重复执行预备语句 $result = $stmt->execute(); if($result) echo "插入数据成功"; $stmt = $conn->prepare("select * from alfas"); //执行预备语句 $stmt->execute(); //绑定输出变量 $stmt->bind_result($year,$model,$accel); print "<table>\n"; print "<tr><th>model</th><th>0-100 km/h</th></tr>\n"; while ($stmt->fetch()){ //将数据提取到输出变量中 print "<tr><td>$year $model</td><td>{$accel} sec</td>\n"; } print "</table>\n";

 

posted @ 2020-08-19 11:48  不知起什么名字  阅读(148)  评论(0编辑  收藏  举报