Get all mysql selected rows into an array

php mysqli fetch all rows into array
mysql fetch array
php pdo fetch all rows into array
sql fetch all rows
php mysqli fetch array multiple rows
fetch all records in mysql
call to undefined function mysql_fetch_array
mysqli display all rows

I am wondering if there a function in php that can allow me put all my selected data in an array .Currently i am using mysql_fetch_array and as i have read in the manual,that function won't fetch every record in the table.

$result = mysql_query("SELECT * FROM $tableName");            
$array = mysql_fetch_array($result);

  echo json_encode($array);

I would suggest the use of MySQLi or MySQL PDO for performance and security purposes, but to answer the question:

while($row = mysql_fetch_assoc($result)){
     $json[] = $row;
}

echo json_encode($json);

If you switched to MySQLi you could do:

$query = "SELECT * FROM table";
$result = mysqli_query($db, $query);

$json = mysqli_fetch_all ($result, MYSQLI_ASSOC);
echo json_encode($json );

php - Get all mysql selected rows into an array, I would suggest the use of MySQLi or MySQL PDO for performance and security purposes, but to answer the question: Get all mysql selected rows into an array I am wondering if there a function in php that can allow me put all my selected data in an array .Currently i am using mysql_fetch_array and as i have read in the manual,that function won't fetch every record in the table.

  1. Loop through the results and place each one in an array

  2. use mysqli_fetch_all() to get them all at one time

Php/mysql fetch all rows in array? - PHP, How can i fetch all the rows in an array. $sql = "SELECT * FROM members WHERE name LIKE 'jim'"  I have a table where two people may have the same name, but separate IDs. I am given the name, and need to request their IDs. When I use a command like: SELECT id_num INTO cust_id FROM Customers

You could try:

$rows = array();
while($row = mysql_fetch_array($result)){
  array_push($rows, $row);
}
echo json_encode($rows);

PHP mysqli fetch_all() Function, Fetch all rows and return the result-set as an associative array: <?php $mysqli echo "Failed to connect to MySQL: " . $mysqli $sql = "SELECT Lastname, Age FROM Persons ORDER BY Lastname"; You need to get all the data that you want from the table. Something like this would work: $SQLCommand = "SELECT someFieldName FROM yourTableName"; This line goes into your table and gets the data in 'someFieldName' from your table.

$name=array(); 
while($result=mysql_fetch_array($res)) {
    $name[]=array('Id'=>$result['id']); 
    // here you want to fetch all 
    // records from table like this. 
    // then you should get the array 
    // from all rows into one array 
}

mysql_fetch_array - Manual, mysql_fetch_array — Fetch a result row as an associative array, a numeric See also MySQL: choosing an API guide and related FAQ for more information. I have found a way to put all results from the select query in an array in one line. Append each loop iteration's text to an array, which you can then echo later using implode(). Dump all the raw results into a multi-dim array using fetch_all(), then loop on that array when ready to echo output.

you can call mysql_fetch_array() for no_of_row time

mysql_fetch_row - Manual, mysql_fetch_row — Get a result row as an enumerated array. Warning. This extension was deprecated in PHP 5.5.0,  The fetch_all () / mysqli_fetch_all () function fetches all result rows and returns the result-set as an associative array, a numeric array, or both. Note: This function is available only with MySQL Native Driver.

Group MySQL rows in an array by column value?, To group rows in an array, use GROUP_CONCAT() along with the ORDER BY clause. Display all records from the table using select statement − mysql> select *from DemoTable;. MySQL SELECT INTO multiple variables example. To store values from the select list into multiple variables, you separate variables by commas. For example, the following statement finds the city and country of the customer number 103 and stores the data in two corresponding variables @city and @country:

MySQL Tutorial - Fetch Array, MySQL Fetch Array. MySQL Here is a sample SELECT query of a table we created in the MySQL Create Table lesson. Here is the code that will print out all the rows of our MySQL Resource. Returns an numerical array of strings that corresponds to the fetched row, or FALSE if there are no more rows. mysql_fetch_row () fetches one row of data from the result associated with the specified result identifier. The row is returned as an array. Each result column is stored in an array offset, starting at offset 0.

How to Select Data from MySQL Database Tables Using PHP , Each time mysqli_fetch_array() is invoked, it returns the next row from the result set as an array. The while loop is used to  When you write stored procedures in MySQL or MariaDB, one of the features you may miss is arrays. At least, at a superficial look – because an array data type is actually not implemented. But actually JSON support was added in MySQL 8.0 and in MariaDB 10.2, and JSON can contain arrays.

Comments
  • Those mysql_... functions are old and unsafe. You should used mysqli or better PDOs - php.net/manual/en/book.mysqli.php, php.net/manual/en/book.pdo.php
  • As Raeki said, mysql are quite old, but other than that, if you still want to loop trough the result, you can do it with following code: $allRows = array(); while($row = mysql_fetch_array($result) {$allRows[] = $row }
  • Please, don't use mysql_* functions for new code. They are no longer maintained and the community has begun the deprecation process. See the red box? Instead you should learn about prepared statements and use either PDO or MySQLi. If you can't decide, this article will help to choose. If you care to learn, here is good PDO tutorial.
  • In case you're copy/pasting, toske needs to add a closing ) for his while. $allRows = array(); while($row = mysql_fetch_array($result)) {$allRows[] = $row }