Jump to content

Putting an entire database into an array?


fluteflute

Recommended Posts

I wrote the following functions to get data from a database. In particular they return data as an array.The first works fine but the second gives me errors about allowed memory size. I googled this and found how to increase the available memory. However this is a small table with around eight columns and only four rows so far. I am concerned for far bigger tables I would need lots of memory. Is there a better way of doing this?

 

Code causing message:

$data = database_getAllRows('users');

 

Functions:

function database_getRow($table, $column, $searchValue)
{
  include('settings.php'); 

  $cxn = mysqli_connect($database_host, $database_user, $database_password, $database_name)

  or die ("Coudn't connect to server.");



  $query = "SELECT * FROM $table WHERE $column='$searchValue'";



  $result = mysqli_query($cxn, $query)

  or die ("<p>Couldn't execute query.");

  while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC))
  {
    return $row; // returns array of row from database

  }
}

function database_getAllRows($table)
{
  include('settings.php'); 

  $cxn = mysqli_connect($database_host, $database_user, $database_password, $database_name)

  or die ("Coudn't connect to server.");



  $query = "SELECT * FROM $table";



  $result = mysqli_query($cxn, $query)

  or die ("<p>Couldn't execute query.");

  while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)!=='NULL')
  {
    $array[] = $row; // creates multidimesnional array of all rows from database

  }
  
  return $array;
}

Link to comment
https://forums.phpfreaks.com/topic/99015-putting-an-entire-database-into-an-array/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.