1. 程式人生 > >mysqli_query和mysql_query的一點區別

mysqli_query和mysql_query的一點區別

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\n", 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.\n"); } /* Select queries return a resultset */ if ($result = $mysqli->query("SELECT Name FROM City LIMIT 10")) {    printf("Select returned %d rows.\n", $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\n", $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\n", 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.\n");
}


/* Select queries return a resultset */
if ($result = mysqli_query($link, "SELECT Name FROM City LIMIT 10")) {
    printf("Select returned %d rows.\n", 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\n", mysqli_error($link));
    }
     mysqli_free_result($result);
 }


     mysqli_close($link);
?>