Martijn Martijn - 12 days ago 4
MySQL Question

Is it possible to select all id's without a while-loop

I'm not sure if there is an answer for this, but I was thinking about the fact that PHP isn't very fast with while loops. That got me thinking about a basic function, which does something like this:

$array = array();
$qItem = "SELECT id FROM someTable";
$sItem = DB::cms_query($qItems);
while($fItem = $sItem->fetch_assoc() ){
$array[] - $fItem['id']; // store the id in an array
}


That will give me an array with all found id's. What I was wondering is this:

Is it possible to select all those id's in 1 query, and only use on fetch (so no loops to fetch), without creating difficult code. The goal is a simple piece of code to select all id's (in this case)

Small edit: I could replace
fetch_assoc()
with
fetch_row()
to improve a tiny bit, but I'm looking to replace the loop


To answer some questions:

I'm using mysqli, in my own custom database class (I'm neq to OO, so in time I might improve that, but thats another story)

Answer

No, the mysqli extension doesn't provide any way to fetch all rows with a single operation, you can only fetch them one at a time. The only PHP API that currently offers this feature is PDO.

Comments