mysqli_fetch_lengths()函數(shù)返回結(jié)果集中的字段長(zhǎng)度。
PHP結(jié)果對(duì)象(mysqli_result類)表示由SELECT或DESCRIBE或EXPLAIN查詢返回的MySQL結(jié)果。
mysqli_fetch_lengths()函數(shù)接受結(jié)果對(duì)象作為參數(shù),檢索給定結(jié)果當(dāng)前行中列的長(zhǎng)度,并以數(shù)組形式返回它們。
mysqli_fetch_lengths($result);
序號(hào) | 參數(shù)及說明 |
---|---|
1 | result(必需) 這是表示結(jié)果對(duì)象的標(biāo)識(shí)符。 |
PHP mysqli_fetch_lengths()函數(shù)返回一個(gè)數(shù)組(整數(shù)),該數(shù)組包含(當(dāng)前行的)每列的長(zhǎng)度/大小(如果成功),并且在出現(xiàn)錯(cuò)誤的情況下返回布爾值FALSE。
此函數(shù)最初是在PHP版本5中引入的,并且可以在所有更高版本中使用。
以下示例演示了mysqli_fetch_lengths()函數(shù)的用法(面向過程風(fēng)格)-
<?php $con = mysqli_connect("localhost", "root", "password", "mydb"); mysqli_query($con, "CREATE TABLE myplayers(ID INT, First_Name VARCHAR(255), Last_Name VARCHAR(255), Place_Of_Birth VARCHAR(255), Country VARCHAR(255))"); print("創(chuàng)建表.....\n"); mysqli_query($con, "INSERT INTO myplayers values(1, 'Sikhar', 'Dhawan', 'Delhi', 'India')"); mysqli_query($con, "INSERT INTO myplayers values(2, 'Jonathan', 'Trott', 'CapeTown', 'SouthAfrica')"); mysqli_query($con, "INSERT INTO myplayers values(3, 'Kumara', 'Sangakkara', 'Matale', 'Srilanka')"); print("插入記錄.....\n"); //檢索表的內(nèi)容 $res = mysqli_query($con, "SELECT * FROM myplayers"); //行的長(zhǎng)度 $row = mysqli_fetch_row($res); $lengths = mysqli_fetch_lengths($res); print_r($lengths); mysqli_free_result($res); mysqli_close($con); ?>
輸出結(jié)果
創(chuàng)建表..... 插入記錄..... Array ( [0] => 1 [1] => 6 [2] => 6 [3] => 5 [4] => 5 )
在面向?qū)ο箫L(fēng)格中,此函數(shù)的語法為$result->lengths;。以下是面向?qū)ο箫L(fēng)格中此函數(shù)的示例;
<?php //建立連接 $con = new mysqli("localhost", "root", "password", "mydb"); $con -> query("CREATE TABLE Test(Name VARCHAR(255), AGE INT)"); $con -> query("insert into Test values('Raju', 25),('Rahman', 30),('Sarmista', 27)"); print("創(chuàng)建表.....\n"); $stmt = $con -> prepare( "SELECT * FROM Test WHERE Name in(?, ?)"); $stmt -> bind_param("ss", $name1, $name2); $name1 = 'Raju'; $name2 = 'Rahman'; //執(zhí)行語句 $stmt->execute(); //檢索結(jié)果 $res = $stmt->get_result(); $row = $res->fetch_row(); $len = $res ->lengths; print_r($len); //結(jié)束語句 $stmt->close(); //關(guān)閉連接 $con->close(); ?>
輸出結(jié)果
創(chuàng)建表..... Array ( [0] => 4 [1] => 0 )
此函數(shù)要檢索長(zhǎng)度,必須在使用 mysqli_fetch_row 或 mysqli_fetch_array 或 mysqli_fetch_object 方法讀取行后調(diào)用,否則返回false:
<?php $con = mysqli_connect("localhost", "root", "password", "mydb"); mysqli_query($con, "CREATE TABLE data(ID INT, Name VARCHAR(255), Age INT)"); mysqli_query($con, "INSERT INTO data values(1, 'Mohan', 25)"); mysqli_query($con, "INSERT INTO data values(2, 'Syamala', 36)"); print("插入記錄.....\n"); //檢索表的內(nèi)容 $res = mysqli_query($con, "SELECT * FROM data"); //行的長(zhǎng)度 $bool = $lengths = mysqli_fetch_lengths($res); if($bool){ print("檢索到長(zhǎng)度"); }else{ print("失敗"); } print_r($lengths); mysqli_free_result($res); mysqli_close($con); ?>
輸出結(jié)果
插入記錄..... 失敗測(cè)試看看?/?