English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Tutorial di base PHP

Tutorial avanzato PHP

PHP & MySQL

Manuale di riferimento PHP

Uso e esempio della funzione PHP mysqli_fetch_all()

PHP MySQLi Manuale di Referenza

La funzione mysqli_fetch_all() recupera tutte le righe dal set di risultati come array associativo, array numerico o entrambi.

Definizione e uso

L'oggetto di risultato PHP (classe mysqli_result) rappresenta il risultato MySQL, restituito da una query SELECT, DESCRIBE o EXPLAIN.
La funzione mysqli_fetch_all ha la seguente funzione: accetta un oggetto di risultato come parametro e ricava tutte le righe dell'oggetto di risultato specificato.

Sintassi

mysqli_fetch_all($result, [$type]);

Parametro

Numero di sequenzaParametri e descrizione
1

result (obbligatorio)

Questo è l'identificatore dell'oggetto di risultato.

2

type (obbligatorio)

Questo è un valore intero che specifica il tipo di array da restituire. Il valore di type è uno dei seguenti:

  • MYSQLI_ASSOC

  • MYSQLI_NUM

  • MYSQLI_BOTH

返回值

PHP mysqli_fetch_all()函数返回一个数组(关联或数字),该数组包含结果对象的行。

PHP版本

此函数最初是在PHP版本5中引入的,并且可以在所有更高版本中使用。

Esempio online

以下示例演示了mysqli_fetch_all()用法说明(面向过程风格),从结果集中取得所有行作为关联数组:

<?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("Creare tabella.....\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");
   //检索表的内容
   $res = mysqli_query($con, "SELECT * FROM myplayers");
   //获取结果的所有行
   $rows = mysqli_fetch_all($res);
   print_r($rows);
   //Fine dello statement
   mysqli_free_result($res);
   //Chiudere la connessione
   mysqli_close($con);
?>

Risultato di output

Creare tabella.....
插入记录.....
Array
(
    [0] => Array
        (
            [0] => 1
            [1] => Sikhar
            [2] => Dhawan
            [3] => Delhi
            [4] => India
        )
    [1] => Array
        (
            [0] => 2
            [1] => Jonathan
            [2] => Trott
            [3] => Città del Capo
            [4] => Sudafrica
        )
    [2] => Array
        (
            [0] => 3
            [1] => Kumara
            [2] => Sangakkara
            [3] => Matale
            [4] => Sri Lanka
        )
)

Esempio online

Nello stile orientato agli oggetti, la sintassi di questa funzione è$result->fetch_all();。Di seguito è riportato un esempio di questa funzione in stile orientato agli oggetti, che recupera tutte le righe dal set di risultati come array associativo:

<?php
   //Stabilire la connessione
   $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("Creare tabella.....\n");
   $stmt = $con->prepare("SELECT * FROM Test WHERE Name in(?, ?)");
   $stmt->bind_param("ss", $name1, $name2);
   $name1 = 'Raju';
   $name2 = 'Rahman';
   //Eseguire lo statement
   $stmt->execute();
   //Ricerca dei risultati
   $result = $stmt->get_result();
   //Leggere tutte le righe
   $rows = $result->fetch_all();
   print_r($rows);
   //Fine dello statement
   $stmt->close();
   //Chiudere la connessione
   $con->close();
?>

Risultato di output

Creare tabella.....
Array
(
    [0] => Array
        (
            [0] => Raju
            [1] => 25
        )
    [1] => Array
        (
            [0] => Rahman
            [1] => 30
        )
)

PHP MySQLi Manuale di Referenza