LAMP开发精要(13):PHP中使用mysql_stmt(预处理语句)处理select查询结果
2010-04-29 01:38
1331 查看
许多PHP脚本通常都会执行除参数以外,其他部分完全相同的查询语句,针对这种重复执行一个查询,每次迭代使用不同的参数情况,MySQL 从4.1版本开始提供了一种名为预处理语句(prepared statement)的机制。它可以将整个命令向MySQL服务器发送一次,以后只有参数发生变化,MySQL服务器只需对命令的结构做一次分析就够了。这不仅大大减少了需要传输的数据量,还提高了命令的处理效率。可以用mysqli扩展模式中提供的mysqli_stmt类的对象,去定义和执行参数化的 SQL命令。以下是使用这种机制实现的一个查询过程。
例子1:
<?php
$db = new mysqli("localhost","user","password","testdb");
if (mysqli_connect_errno()){
printf("Error:%s/n",mysqli_connect_error());
exit;
}
else
{
if ($stmt = $db->prepare("select id,name,author,price from book where name like ?"))
{
$stmt->bind_param('s',$n);
$n = "%p%";
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($id,$name,$author,$price);
while ($stmt->fetch())
{
printf("%s:%s,%s,%s<br/>",$id,$name,$author,$price);
}
$stmt->close();
}
$db->close();
}
?>
例子2. Object oriented style
<?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();
}
$stmt = $mysqli->prepare("INSERT INTO CountryLanguage VALUES (?, ?, ?, ?)");
$stmt->bind_param('sssd', $code, $language, $official, $percent);
$code = 'DEU';
$language = 'Bavarian';
$official = "F";
$percent = 11.2;
/* execute prepared statement */
$stmt->execute();
printf("%d Row inserted./n", $stmt->affected_rows);
/* close statement and connection */
$stmt->close();
/* Clean up table CountryLanguage */
$mysqli->query("DELETE FROM CountryLanguage WHERE Language='Bavarian'");
printf("%d Row deleted./n", $mysqli->affected_rows);
/* close connection */
$mysqli->close();
?>
例子3. Procedural style
<?php
$link = mysqli_connect('localhost', 'my_user', 'my_password', 'world');
/* check connection */
if (!$link) {
printf("Connect failed: %s/n", mysqli_connect_error());
exit();
}
$stmt = mysqli_prepare($link, "INSERT INTO CountryLanguage VALUES (?, ?, ?, ?)");
mysqli_stmt_bind_param($stmt, 'sssd', $code, $language, $official, $percent);
$code = 'DEU';
$language = 'Bavarian';
$official = "F";
$percent = 11.2;
/* execute prepared statement */
mysqi_stmt_execute($stmt);
printf("%d Row inserted./n", mysqli_stmt_affected_rows($stmt));
/* close statement and connection */
mysqli_stmt_close($stmt);
/* Clean up table CountryLanguage */
mysqli_query($link, "DELETE FROM CountryLanguage WHERE Language='Bavarian'");
printf("%d Row deleted./n", mysqli_affected_rows($link));
/* close connection */
mysqli_close($link);
?>
二例将输出:
1 Row inserted.
1 Row deleted.
例子1:
<?php
$db = new mysqli("localhost","user","password","testdb");
if (mysqli_connect_errno()){
printf("Error:%s/n",mysqli_connect_error());
exit;
}
else
{
if ($stmt = $db->prepare("select id,name,author,price from book where name like ?"))
{
$stmt->bind_param('s',$n);
$n = "%p%";
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($id,$name,$author,$price);
while ($stmt->fetch())
{
printf("%s:%s,%s,%s<br/>",$id,$name,$author,$price);
}
$stmt->close();
}
$db->close();
}
?>
例子2. Object oriented style
<?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();
}
$stmt = $mysqli->prepare("INSERT INTO CountryLanguage VALUES (?, ?, ?, ?)");
$stmt->bind_param('sssd', $code, $language, $official, $percent);
$code = 'DEU';
$language = 'Bavarian';
$official = "F";
$percent = 11.2;
/* execute prepared statement */
$stmt->execute();
printf("%d Row inserted./n", $stmt->affected_rows);
/* close statement and connection */
$stmt->close();
/* Clean up table CountryLanguage */
$mysqli->query("DELETE FROM CountryLanguage WHERE Language='Bavarian'");
printf("%d Row deleted./n", $mysqli->affected_rows);
/* close connection */
$mysqli->close();
?>
例子3. Procedural style
<?php
$link = mysqli_connect('localhost', 'my_user', 'my_password', 'world');
/* check connection */
if (!$link) {
printf("Connect failed: %s/n", mysqli_connect_error());
exit();
}
$stmt = mysqli_prepare($link, "INSERT INTO CountryLanguage VALUES (?, ?, ?, ?)");
mysqli_stmt_bind_param($stmt, 'sssd', $code, $language, $official, $percent);
$code = 'DEU';
$language = 'Bavarian';
$official = "F";
$percent = 11.2;
/* execute prepared statement */
mysqi_stmt_execute($stmt);
printf("%d Row inserted./n", mysqli_stmt_affected_rows($stmt));
/* close statement and connection */
mysqli_stmt_close($stmt);
/* Clean up table CountryLanguage */
mysqli_query($link, "DELETE FROM CountryLanguage WHERE Language='Bavarian'");
printf("%d Row deleted./n", mysqli_affected_rows($link));
/* close connection */
mysqli_close($link);
?>
二例将输出:
1 Row inserted.
1 Row deleted.
相关文章推荐
- php中使用mysql_stmt(预处理语句)来处理select查询结果
- mysqli_stmt类:使用预处理语句处理SELECT查询结果
- php中使用mysql_stmt(预处理语句)3个小时的艰难故障排除小结
- php中使用mysql_stmt(预处理语句)
- MYSQL 用 explain 语句判断select查询是否使用了索引
- Php中使用Select 查询语句的实例
- Mac OS使用brew安装Nginx、MySQL、PHP-FPM的LAMP开发环境
- php分页查询mysql结果的base64处理方法示例
- php+mysqli使用预处理技术进行数据库查询的方法
- MySQL使用select语句查询指定表中指定列(字段)的数据
- Php中使用Select 查询语句的实例
- MySQL解析器源码分析--对select语句中子查询处理逻辑的分析(二)
- (转)PL/SQL编辑数据"这些查询结果不可更新,请包括ROWID或使用SELECT...FOR UPDATE获得可更新结果"处理
- Mac OS使用brew安装Nginx、MySQL、PHP-FPM的LAMP开发环境
- Php中使用Select 查询语句的实例
- php分页查询mysql结果base64处理
- php+mysqli使用预处理技术进行数据库查询的方法
- PL/SQL编辑数据"这些查询结果不可更新,请包括ROWID或使用SELECT...FOR UPDATE获得可更新结果"处理
- Mac OS使用brew安装Nginx、MySQL、PHP-FPM的LAMP开发环境
- PHP使用mysql_fetch_object从查询结果中获取对象集的方法