您的位置:首页 > 编程语言 > PHP开发

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.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息