简体   繁体   中英

Retrieving mysql data on to one php page from multiple tables

I am a student still new to php and mysql,i am developing a website which is database driven. It has two pages ie index.php and subpage.php. On the index page there appears 14 links namely about_us,projects,services,partners and among others. And for the case of the subpage.php, it has "a blank body",the header, navigation and the footer.

My goal is to not to create pages for every link that appears on the index.php. So i want to use only the "blank body" in the subpage.php to display the data for every link that is on the index.php whenever it is clicked on.

In my struggle to achieve this, i have created a database with 14 tables so that each should cater for every link on the index.php.

So I would like you guys to help me how i can RETRIEVE DATA FROM THE DATABASE FROM DIFFERENT TABLES ON TO THE SUBPAGE.PHP

Forexample; If am am on the index.php and i click about_us, it should ONLY retrieve data from the table called about us in the database. And if i click another link it should on retrieve data specifically for that link i have clicked on.

Here is the sample php codes that am using to retrieve data for only projects_table onto the subpage.php

<?php
require_once("connection.php");
?>

<table>
<?php
mysql_select_db("cognative_db",$sql);
$sql="SELECT * FROM projects_table ";
$result=mysql_query($sql);


while($row=mysql_fetch_array($result)){
?>
<tr>
<td><?php echo $row['project_title'];?></td>
<tr>
<td><?php echo $row['project_details'];?>
 <?php echo " ";?>
 </td>

</tr>

<?php
}
?>
<?php
mysql_close();
?>

</table>

Any help will be highly appreciated

I dont know why for every link you made the table. but justust for displaying multiple table data, you can pass table name in query string as:

<a href="about_us.php?q=about_us">Click</a>

// and on subpage.php retirieve it by get method as:

$table=$_GET['q'];

//and make your sql query 
$sql="SELECT * FROM $table";

I can't agree with your database design concepts. But to problem of having one page for all links , one way can be - you can create links like this

http://yoursite/subpage.php?page=content
http://yoursite/subpage.php?page=aboutus

On your subpage.php

$page= $_GET['page'];

depending upon the value of $page you can choose which query to run and what to display in the page.

To add to all these, you don't need to have 14 tables to cater 14 links. Perhaps you can start looking at Joins

simply create simple table structure for your website,like below one instead of assigning one menu one table

     |id    | menu      | content   |
     |1     | about_us  | asds....  |
     |2     | services  | asds....  |

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM