Home  >  Article  >  Backend Development  >  Preprocessing technology for MYSQL operations in PHP (2) Database dql query statement_PHP tutorial

Preprocessing technology for MYSQL operations in PHP (2) Database dql query statement_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:19:511102browse

Preprocessing technology for MYSQL operations in php (2) Database dql query statement

<?php
//预处理技术

//创建一个mysqli对象
$mysqli = new MySQLi("主机名","mysql用户名","密码","数据库名");

//判断是否链接成功
if($mysqli->connect_error){
	die($mysqli->connect_error);
}

//创建预编译对象
$sql = "select id,name,age,qq from 表名 where id<?";
$mysqli_compile = $mysqli->prepare($sql);

//绑定参数
$id=10;

//给?处进行赋值,"ssi"指string,string,int,数据类型和顺序一一对应
//bind_param()这里参数数目是可变。
$mysqli_compile->bind_param("i",$id);

//绑定结果集,这里是用引用传参的方式
$mysqli_compile->bind_result($name,$age,$qq);

//执行语句
$res = $mysqli_compile->execute();

//失败打印出原因
if(!$res){
	die("失败原因=".$mysqli_compile-error);
}

//取出绑定结果值
while($mysqli_compile->fetch()){
	echo "--$id--$name--$age--$qq";
}

//如果还要取其他的结果可以再次绑定参数取结果,但不用绑定结果集

//释放结果集
$mysqli_compile->free_result();

//关闭资源,除去数据库的预编译的指令
$mysqli_compile->close();

//关闭链接资源
$mysqli->close();
?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/871199.htmlTechArticlePreprocessing technology for MYSQL operations in php (2) Database dql query statement connect_error){die($mysqli- >connect_error);}//Create precompiled object $sql = "select id,name,age,qq from table name w...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn