Jump to content

[SOLVED] PHP/MySQL Help - Selecting whole Database VS one table


dennismonsewicz

Recommended Posts

Hello,

 

My question is a little complicated to ask so I will try to ask it to make as much sense as possible LOL.

 

OK here goes...

 

In querying a regular DB you use the following code:

$query = "SELECT * FROM table_name";

 

Well I want to be able to do something like this:

$query = "SELECT * FROM database_name";

 

Here is my reasoning:

 

I have built and intranet for the company I work for. I have imported all of the data requested from an Excel Spreadsheet into the corresponding SQL tables. Now for the page I am wanting to pull off my weird select db_name code works like this:

 

I want to be able to go there and display the Table names within that DB and when a user clicks on one of the links it will reload the page displaying the results for the corresponding Table. Make sense?

 

Thanks for all help,

 

Dennis

 

PS - I am using MySQL. Oh and if you want to contact me directly PLEASE email me at dennismonsewicz@gmail.com

Link to comment
Share on other sites

SHOW TABLES

Will return all of the tables in a database.  So you initially do that and build a link for each individual table, something like:

http://www.domain.com/viewtable.php?table=table_name

 

In viewtable.php, you get the table name from $_GET['table'] and run the query:

  SELECT * FROM {$_GET['table']}

DO NOT FORGET TO SANITIZE ANY DATA BEFORE PUTTING IT IN THE DB, MY CODE IS JUST A SAMPLE, NOT MEANT TO BE USED AS IS.

 

Since the columns will vary depending on which table is selected, when displaying the table you can do:

$q = mysql_query("SELECT * FROM {$_GET['table']}");
if($q){
  echo '<table>';
  $bHdrs = false;
  while($row = mysql_fetch_assoc($q)){
    // This IF will print the table headers on the first row encountered
    if(!bHdrs){
      $bHdrs = true;
      $tmp = Array();
      foreach($row as $k => $v){
        $tmp[] = $k;
      }
      echo '<tr><td>' . implode('</td><td>', $tmp) . '</td></tr>';
    }
    echo '<tr><td>' . implode('</td><td>', $row) . '</td></tr>';
  }
  echo '</table>';
}

 

Again, my code is just a sample.  Remember to sanitize your data before using it in queries!

Link to comment
Share on other sites

Howdy,

 

I follow all of your code up to this part:

 

$bHdrs = false;
  while($row = mysql_fetch_assoc($q)){
    // This IF will print the table headers on the first row encountered
    if(!bHdrs){
      $bHdrs = true;
      $tmp = Array();
      foreach($row as $k => $v){
        $tmp[] = $k;
      }
      echo '<tr><td>' . implode('</td><td>', $tmp) . '</td></tr>';
    }
    echo '<tr><td>' . implode('</td><td>', $row) . '</td></tr>';

 

what is the bHdrs variable used for? what does the implode function do? and what is the $tmp variable for?

 

Sorry If I sound like a complete idiot...I am still learning. I just learned how to do a full text search yesterday. So I am trying to ask as many questions as possible :)

 

Thanks,

 

Dennis

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.