mysql预编译处理(mysqli、PDO)
2015-06-24 15:58
721 查看
DML语句预编译:
MysqLi:
PDO:
DQL语句预编译:
mysqli:
MysqLi:
<?php $mysqli = new mysqli("localhost","root","root","dbname"); $mysqli->query("set names utf8"); $sql = 'insert into user(id,name,age,email) values (?,?,?,?)'; $mysqli_stmt = $mysqli->prepare($sql); $id = 2; $name = 'kung'; $age = 28; $email = 'ohdas@163.com'; $mysqli_stmt->bind_param('isis',$id,$name,$age,$email); $res = $mysqli_stmt->execute(); if(!$res){ echo 'error'.$mysqli_stmt->error; exit; }else{ echo 'ok'; } $id = 3; $name = 'xiaoyu'; $age = 28; $email = 'kung-yu@163.com'; $mysqli_stmt->bind_param('isis',$id,$name,$age,$email); $res = $mysqli_stmt->execute(); if(!$res){ echo 'error'.mysqli_stmt->error; exit; }else{ echo 'ok'; } ?>
PDO:
<?php $dns = 'mysql:dbname=dbname;host=127.0.0.1'; $user = 'root'; $password = 'root'; try{ $pdo = new PDO($dns,$user,$password); } catch(PDOException $e){ echo $e->getMessage(); } $pdo->query("set names utf8"); $sql = 'inser into user values(:id,:name,:age,:email)'; $pdo_stmt = $pdo->prepare($sql); $id = 2; $name = 'kung'; $age = 27; $email = 'ohdas@163.com'; $pdo_stmt->bindParam(':id',$id); $pdo_stmt->bindParam(':name',$name); $pdo_stmt->bindParam(':age',$age); $pdo_stmt->bindParam(':email',$email); $pdo_stmt->execute(); ?>
DQL语句预编译:
mysqli:
<?php $mysqli = new mysqli("localhost","root","root","dbname"); $mysqli->query("set names utf8"); $sql = " select id,name from user where id > ?"; $mysqli_stmt = $mysqli->prepare($sql); $id = 1; $mysqli_stmt->bind_param('i',$id); $mysqli_stmt->bind_result($id,$name); $mysqli_stmt->execute(); while($mysqli_stmt->fetch()){ echo $id.'--'.$name; } $mysqli_stmt->close(); $mysqli->close(); ?>
相关文章推荐
- Mysql 查询表中重复数据和删除表中重复的数据
- Mysql主从状态的简单监视
- lnmp 环境搭建 源代码包安装 linux,nginx,mysql,php
- MySQL中用decimal的原因
- 解决Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in
- mysql远程登录权限不足问题解决
- (转)理解MySQL——索引与优化
- MySQL can’t specify target table for update in FROM clause
- MySQL优化之——自定义存储过程和函数
- mysql数据库隔离级别
- 如何启动/停止/重启MySQL
- Mysql中的循环语句
- mysql开启日志sql语句
- MySQL的varchar定义长度到底是字节还是字符
- Mysql跨表更新多表update sql语句
- mysql分析函数的实现
- 30多条mysql数据库优化方法,千万级数据库记录查询轻松解决
- mysql双机,多机异地热备简单原理实战
- MySQL删除存储过程和函数
- <笔记>mysql的启动