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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.