mysql绑定变量的使用方法
<?php $mysqli = new mysqli("localhost", "my_user", "my_password", "world"); /* check connection */ if (mysqli_connect_errno()) { printf("Connect failed: %s\n", mysqli_connect_error()); exit(); } $city = "Amersfoort"; /* create a prepared statement */ if ($stmt = $mysqli->prepare("SELECT District FROM City WHERE Name=?")) { /* bind parameters for markers */ $stmt->bind_param("s", $city); /* execute query */ $stmt->execute(); //下面的变量为查询表中的字段命名的变量 $stmt->bind_result($district); /* fetch value */ $stmt->fetch(); printf("%s is in district %s\n", $city, $district); /* close statement */ $stmt->close(); } /* close connection */ $mysqli->close(); ?>
上面的是mysql中查询的变量帮定使用方法
在mysql的查询中需要将语句先进行解析处理再进行缓存处理,如果在解析的过程中判断该语句已经执行过的话则直接执行,不会再次进行解析
如下面的语句
select * from flight where fnum='MU5674'
select * from fllight where fnum='MU5655'
可能因为一个变量的原因就需要mysql的重新解析 这时就需要mysql的重新解析
如果写成
select * from flight where fnum=?
则此时的效果就不一样了 执行的结果可以通过
mysql> show profiles; +----------+------------+-------------------------------------------+ | Query_ID | Duration | Query | +----------+------------+-------------------------------------------+ | 31 | 0.00007200 | set @s:=3 | | 32 | 0.01539800 | select count(*) from io_01 where c1 = @s | | 33 | 0.01528700 | select count(*) from io_01 where c1 = @s | | 34 | 0.01539700 | select count(*) from io_01 where c1 = @s | | 35 | 0.01587500 | select count(*) from io_01 where c1 = @s | | 36 | 0.01508000 | select count(*) from io_01 where c1 = @s | | 37 | 0.01512300 | select count(*) from io_01 where c1 = @s | | 38 | 0.01506700 | select count(*) from io_01 where c1 = 3 | | 39 | 0.00022400 | select count(*) from io_01 where c1 = 3 | | 40 | 0.00021400 | select count(*) from io_01 where c1 = 3 | | 41 | 0.00004000 | select count(*) from io_01 where c1 = 3 | | 42 | 0.00022300 | select count(*) from io_01 where c1 = 3 | | 43 | 0.00023800 | select count(*) from io_01 where c1 = 3 | | 44 | 0.00022500 | select count(*) from io_01 where c1 = 3 | | 45 | 0.00004000 | select count(*) from io_01 where c1 = 3 | +----------+------------+-------------------------------------------+ 15 rows in set (0.00 sec)
来查询执行的结果 !
以下是mysql绑定变量在增删改方面的运用
<?php $mysqli=new mysqli("localhost", "root", "123321", "test"); $sql1="set @@profiling=1"; $result1=$mysqli->query($sql1); //准备好一条语句放到服务器中,插入语句 $sql="insert into t(name,sex) values (?,?)"; $stmt=$mysqli->prepare($sql); //给占位符号每个?号传值(绑定参数) i d s b $stmt->bind_param("si", $name, $sex); $name="andy"; $sex=0; //执行 $stmt->execute(); $name="mandy"; $sex=1; //执行 $stmt->execute(); $name="michael"; $sex=0; //执行 $stmt->execute(); $name="happy"; $sex=1; //执行 $stmt->execute(); $name="php"; $sex=1; //执行 $stmt->execute(); $name="mysql"; $sex=1; //执行 $stmt->execute(); $name="linux"; $sex=1; //执行 $stmt->execute(); $name="oracle"; $sex=1; //执行 $stmt->execute(); $name="unix"; $sex=1; //执行 $stmt->execute(); $name="cisco"; $sex=1; //执行 $stmt->execute(); $stmt->close(); $sql2="show profiles"; $result2=$mysqli->query($sql2); echo '<table border=1 align="center" width=800>'; while($rows=$result2->fetch_assoc()){ echo '<tr align="center">'; foreach($rows as $value){ echo '<td>' . $value . '</td>'; } echo '</tr>'; $i=0; $i=$i+$rows["Duration"]; } echo '</table>'; echo $i; ?>