Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8072855
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T14:09:34+00:00 2026-06-05T14:09:34+00:00

I want to automatically load ALL my database data into $db[] and/or $_SESSION[‘db’] .

  • 0

I want to “automatically” load ALL my database data into $db[] and/or $_SESSION['db']. Thus, I just have to load this code once, instead of loading bits and pieces throughout the website.

I was able to query the meta data and raw data, my problem is creating the array.

Let’s say I have a table temp with columns name and age I want to be able to do something like this: echo $db['temp']['name'] . "'s age is: " . $db['temp']['age']; -Obviously I have lots of tables with lots of columns. This is just an example

<?php
$dbn = "db_name";
$dbu = "db_user";
$dbp = "db_password";
$hst = 'db_host';

$dbc = mysql_connect($hst,$dbu,$dbp);


$sql_metadata = 
"SELECT `COLUMNS`.`TABLE_NAME` as 'table' , `COLUMNS`.`COLUMN_NAME` as 'column'
FROM `information_schema`.COLUMNS
WHERE `COLUMNS`.`TABLE_SCHEMA` = '$dbn'
ORDER BY TABLE_NAME ASC , `COLUMNS`.`COLUMN_NAME` ASC";

$dbq = mysql_query($sql_metadata, $dbc);
while( $meta = mysql_fetch_array($dbq) )
{
    $sql_rawdata = "SELECT `$meta['column']` FROM `$dbn`.`$meta['table']`";
    $dbq2 = mysql_query($sql_rawdata);
    foreach( ($raw = mysql_fetch_array($dbq2)) as $value) )
    {
        $db = array
                ( 
                $meta['table'] => array( $meta['column'] => $value)
                );
    }
}
print_r($db);
?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T14:09:36+00:00Added an answer on June 5, 2026 at 2:09 pm

    Declare your array before going into the loop and then fill it:

    $db = array();
    
    $sql_metadata = 
    "SELECT DISTINCT `COLUMNS`.`TABLE_NAME` as 'table' 
    FROM `information_schema`.COLUMNS 
    WHERE `COLUMNS`.`TABLE_SCHEMA` = '$dbn'";
    
    $dbq = mysql_query($sql_metadata, $dbc);
    while( $meta = mysql_fetch_array($dbq) )
    {
        $sql_rawdata = "SELECT `$meta['column']` FROM `$dbn`.`$meta['table']`";
        $dbq2 = mysql_query($sql_rawdata);
        $raw = mysql_fetch_array($dbq2);
    
        foreach($raw as $value )
        {
            $db[$meta['table']][$meta['column']][] = $value;
        }
    }
    print_r($db);
    

    For explanation: You create a multi-dimensional array with the table name pointing towards the column names. the [] at the end of the array tells PHP to store the content of $value into a new array element of table=>column.

    THis would be an easier way to fetch the whole data:

        $sql_rawdata = "SELECT * FROM `".$dbn."`.`".$meta['table']."`";
        $dbq2 = mysql_query($sql_rawdata);
    
        while($raw = mysql_fetch_assoc($dbq2)){
            foreach($raw as $column => $value )
            {
                $db[$meta['table']][$column][] = $value;
            }
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want my code to automatically try multiple ways to create a database connection.
I want to automatically fit time series returns into a NIG distribution. With nigfit()
I have an index view that I want to update automatically as the user
I want to generate a classpath automatically with all the *.jar files contained under
Say I wanted to load google, then automatically enter text into the search bar
At the moment I load the database class in my autoload which automatically creates
I want to use Peter Marklund's html test plugin to automatically validate all of
i want to split my views project into sereveral modules. i wanna have a
In Python, when I want to use this COM object, all I do is
i want to automatically run a simple shell script at linux startup i'm working

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.