首页 / MYSQL / MySqli 中预处理类 stmt
MySqli 中预处理类 stmt
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了MySqli 中预处理类 stmt,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2715字,纯文字阅读大概需要4分钟。
内容图文
![MySqli 中预处理类 stmt](/upload/InfoBanner/zyjiaocheng/1122/7ad2e55a6ae44bdaadc5b2f3cb2b53cb.jpg)
非select 语句(没有结果集的)
1.建立连接数据库
$mysqli=new mysqli("localhost","root","","sqldb");
2.判断
if(mysqli_connect_error()){
echo "连接数据库失败".mysqli_connect_error();
exit;
}
3.SQL语句拼装
$sql="insert into shops(name,price,description) values(?,?,?,?)";
$sql="update shops set name=?,price=?,num=?,description=?; where id=?";
4.插入SQL 语句
$stmt=$mysqli->prepare($sql);//比较方便
5.给每一个符号的?传值(绑定参数)
$stmt->bind_param("sdisi",$name,$price,$num,$description,$id);
6.传值
$name="zhangsan";
$price=22.22;
$num=10;
$description="very good";
$id=10;
7.执行
$stmt->execute();
影响行数:$stmt->insert_id;
最后插入的ID:$stmt->offected_rows;
$mysqli=@new mysqli("localhost","root","","sqldb"); if(mysqli_connect_error()){ echo "连接数据库错误".$mysqli_connect_error(); } /*拼装数据*/$sql="insert into shop values (null,?,?,?)"; $sql="update shop set name=?,price=?,description=? where id=?"; /*插入数据*/$stmt=$mysqli->prepare($sql); /*绑定数据*/$stmt->bind_param("sdsi", $name, $price, $description, $id); /*赋值*/$name="zhangsanasdfadfasdfasdf"; $price=2.3; $description="very good"; $id=1; /*执行*/$stmt->execute();
select 语句(有结果集的)
1.连接数据库
$mysqli=new mysqli("localhost","root","","sqldb");
2.判断数据库
if(mysqli_connect_error()){
echo "Error:".mysqli_connect_error();
}
3.拼接数据库
$sql="select id,name,price,description where id=?";
4.插入数据库
$stmt=$myqli->prepare($sql);
5.绑定数据库
$stmt->bind_param(i,$id);
6.绑定结果集
$stmt->bind_result($id,$name,$price,$description);
7.赋值
$id=10;
8.执行
$stmt->execute();
9.取出结果集
$stmt->store_result();
10.while 查看结果
while($stmt->fetch()){
echo "$id---$name---$price--$description";
}
11.关闭结果集
$stmt->free_result();
12.关闭数据库
$stmt->close();
<?php $mysqli= @new MySQLi("localhost","root","","sqldb"); if(mysqli_connect_error()){ echo "Error:".mysqli_connect_error(); } /*拼装SQL*/$sql="select id,name,price,description from shop where id<?"; /*插入*/$stmt=$mysqli->prepare($sql); var_dump($stmt); /*绑定*/$stmt->bind_param(i,$id); /*绑定结果集*/$stmt->bind_result($id,$name,$price,$description); /*赋值*/$id=10; /*执行*/$stmt->execute(); /*取出结果集*/$stmt->store_result(); echo "<table border=‘1‘>"; //字段信息、列信息$result=$stmt->result_metadata(); echo "<tr>"; while($field=$result->fetch_field()){ echo "<th>{$field->name}</th>"; } echo "</tr>"; /*fetch()查看结果集*//*移动数据库指针*///$stmt->data_seek(2);while($stmt->fetch()){ echo "<tr>"; echo "<td>$id</td><td>$name</td><td>$price</td><td>$description</td>"; echo "</tr>"; } echo "</table>"; /*关闭结果集*/$stmt->free_result(); /*关闭数据库d*/$stmt->close(); ?>
原文:http://www.cnblogs.com/subtract/p/3852486.html
内容总结
以上是互联网集市为您收集整理的MySqli 中预处理类 stmt全部内容,希望文章能够帮你解决MySqli 中预处理类 stmt所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。