牛骨文教育服务平台(让学习变的简单)
博文笔记

mysqli_query和mysql_query的一点区别

创建时间:2015-05-06 投稿人: 浏览次数:4378
mysqli是php5提供的新函数库,i表示改进(improve),其执行速度要比mysql_query更快。一般现在php在5或者5以上的版本之中默认支持extension=php_mysql.dll和extension=php_mysqli.dll。 mysql.dll查询方式可以理解为过程化的查询,如下: $conn=mysql_connect("localhost","user","password"); //连接mysql数据库 mysql_select_db("databse");  //选择数据库    $result=mysql_query("select * from table_name");   //mysql_query有两个参数mysql_query(query,connection),第二个参数可选,指明打开连接的名称。 $rzt=mysql_fetch_assoc($qry),$qry是必须的,代表数据指针,该数据指针是从mysql_query()返回的结果集。利用循环语句输出所有语句。 mysqli.dll的查询存在两种方式(网上找到的两个很好的例子):         1、对象方式: <?php

$mysqli = new mysqli("localhost", "my_user", "my_password", "world");

/* check connection */ if (mysqli_connect_errno()) {     printf("Connect failed: %s ", mysqli_connect_error());     exit(); }

/* Create table doesn"t return a resultset */ if ($mysqli->query("CREATE TEMPORARY TABLE myCity LIKE City") === TRUE) {     printf("Table myCity successfully created. "); }

/* Select queries return a resultset */ if ($result = $mysqli->query("SELECT Name FROM City LIMIT 10")) {     printf("Select returned %d rows. ", $result->num_rows);

     /* free result set */     $result->close(); }

/* If we have to retrieve large amount of data we use MYSQLI_USE_RESULT */ if ($result = $mysqli->query("SELECT * FROM City", MYSQLI_USE_RESULT)) {

    /* Note, that we can"t execute any functions which interact with the server until result set was closed. All calls will return an  "out of sync" error */     if (!$mysqli->query("SET @a:="this will not work"")) {         printf("Error: %s ", $mysqli->error);     }     $result->close(); }

$mysqli->close();
?>

       2、过程化方式:

<?php
$link = mysqli_connect("localhost", "my_user", "my_password", "world");


/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s ", mysqli_connect_error());
    exit();
}


/* Create table doesn"t return a resultset */
if (mysqli_query($link, "CREATE TEMPORARY TABLE myCity LIKE City") === TRUE) {
    printf("Table myCity successfully created. ");
}


/* Select queries return a resultset */
if ($result = mysqli_query($link, "SELECT Name FROM City LIMIT 10")) {
    printf("Select returned %d rows. ", mysqli_num_rows($result));


    /* free result set */
    mysqli_free_result($result);
}


/* If we have to retrieve large amount of data we use MYSQLI_USE_RESULT */
if ($result = mysqli_query($link, "SELECT * FROM City", MYSQLI_USE_RESULT)) {


    /* Note, that we can"t execute any functions which interact with the server until result set was closed. All calls will return an  "out of sync" error */
    if (!mysqli_query($link, "SET @a:="this will not work"")) {
        printf("Error: %s ", mysqli_error($link));
    }
     mysqli_free_result($result);
 }


     mysqli_close($link);
?>

声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。