1

Simple question. lets say I run the following:

$results = mysql_query("SELECT * FROM table");

How can I load the mysql results array into a PHP array without doing a while loop? Example:

$mysql_results = array();
$results = mysql_query("SELECT * FROM table");
$mysql_results = mysql_load_all_results_into_array($results);

Thanks!

1
  • There is no such way...If it were You'd probably know of it... :-) Commented Nov 26, 2012 at 16:38

4 Answers 4

1

PHP's mysql_ library does not offer a method to "fetch all" rows without looping through the results-object using mysql_fetch_row() (or similar).

You can, however, update your code to use the mysqli library which contains a method named mysqli_result::fetch_all to perform the task you desire. Alternatively, you could update to use PDO which contains a similar method named PDOStatement::fetchAll.

As the mysql_ methods are being deprecated, updating to mysqli (or PDO) is the recommended way to go regardless.

Sign up to request clarification or add additional context in comments.

Comments

0

You can't do that with mysql. You have to loop. You can do that with PDO

Comments

0

Use MySQLi extension please :

http://php.net/manual/en/mysqli-result.fetch-all.php

Comments

0

use:
$values = mysql_fetch_array($results);
but do check for success of your operation.... thus:
while ($values = mysql_fetch_array($result)) {
    $msg = "Array gotten successfully.";
 }

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.