dennismonsewicz Posted November 28, 2007 Share Posted November 28, 2007 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 Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted November 28, 2007 Share Posted November 28, 2007 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! Quote Link to comment Share on other sites More sharing options...
dennismonsewicz Posted November 28, 2007 Author Share Posted November 28, 2007 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 Quote Link to comment Share on other sites More sharing options...
dennismonsewicz Posted November 28, 2007 Author Share Posted November 28, 2007 Also does $_GET['table'] pull all the names of the tables or do i need to replace it with something? Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted November 28, 2007 Share Posted November 28, 2007 Let's take this one step at a time. Are you yet familiar with url parameters and $_GET? Quote Link to comment Share on other sites More sharing options...
dennismonsewicz Posted November 29, 2007 Author Share Posted November 29, 2007 Yes... Quote Link to comment Share on other sites More sharing options...
dennismonsewicz Posted November 29, 2007 Author Share Posted November 29, 2007 Sorry it took me so long to get back with you....I had to leave the office cause my head was hurting yesterday after looking at so much code. LOL Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted November 29, 2007 Share Posted November 29, 2007 You've marked the topic as solved, does that mean you no longer need help? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.