0

Currently, I have coded this (in PHP).

<? $array = array("0");

$q1 =mysql_query("SELECT id FROM `stuff` ORDER BY `id` ASC");

while($q=mysql_fetch_object($q1)){
$array[] = $q->id;
} ?>

It doesn't take that long to load, but I was wondering, if there was a more efficient way than this? If there is, please share. It just doesn't feel efficient if there are 1000 rows.

If there isn't, please say so.

3
  • 2
    What are trying to do? Do you really need all IDs and "0"? Commented Jul 6, 2014 at 15:29
  • Note: the mysql extension is deprecated as of PHP 5.5. Use PDO or mysqli. PDO for example is offers an API to directly bind a result to an object (or change it according to the result of the query) Commented Jul 6, 2014 at 15:31
  • Question solved. I'll take your advice Rangad. But, if I do, then I will have to convert thousands of lines of code sigh, but I guess it will be for the long term benefit. Commented Jul 6, 2014 at 15:59

1 Answer 1

1

you can use mysqli_fetch_all rather than looping through each row:

$q1 ="SELECT id FROM `stuff` ORDER BY `id` ASC";
$result=mysqli_query($con,$q1);
$array=mysqli_fetch_all($result,MYSQLI_ASSOC);
Sign up to request clarification or add additional context in comments.

1 Comment

But this will return a two-dimensional array, each element an array with key 'id', while the OP is looking for a straightforward array of the values.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.