LAMP开发精要(13):PHP中使用mysql_stmt(预处理语句)处理select查询结果

    许多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.

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值