This topic is locked

How to retrieve data from sql query with multiple record, in a two dimensional array

1/11/2025 12:00:21 AM
PHPRunner General questions
A
Abu author

$sql = DB::Query("Select name, id from list where location=1");
//since this query only return one record I parse the value by fetchAssoc()
$data = $sql->fetchAssoc();
$name = $data["name"];
$id = $data["id"];

//How do I parse the data values if the query returns 3 records?
.
.
.
?,

C
Chris Whitehead 1/11/2025

I hope this is what you're after.

$sql = DB::Query("Select name, id from list where location=1");
while( $data = $sql->fetchAssoc() )
{
$name = $data["name"];
$id = $data["id"];
// process the $id and $name
}

If you want to store in another array.

$sql = DB::Query("Select name, id from list where location=1");
$records = array();
while( $data = $sql->fetchAssoc() )
{
$records[] = $data;
}

// you can return the records array or process it.
return $records;