简体   繁体   English

从多个表中将mysql数据检索到一个php页面上

[英]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. 我是一个仍不熟悉php和mysql的学生,我正在开发一个由数据库驱动的网站。 It has two pages ie index.php and subpage.php. 它有两个页面,即index.php和subpage.php。 On the index page there appears 14 links namely about_us,projects,services,partners and among others. 在索引页面上,出现14个链接,即about_us,项目,服务,合作伙伴等。 And for the case of the subpage.php, it has "a blank body",the header, navigation and the footer. 对于subpage.php,它具有“空白正文”,页眉,导航和页脚。

My goal is to not to create pages for every link that appears on the index.php. 我的目标是不为出现在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. 因此,我只想在subpage.php中使用“空白正文”来显示每次单击index.php上每个链接的数据。

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. 为了实现这一目标,我创建了一个包含14个表的数据库,以便每个表都可以满足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 因此,我希望你们能帮助我如何从不同的表到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. 如果上午在index.php上,并且我单击about_us,则只能从数据库中关于我们的表中检索数据。 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 这是用于仅将projects_table的数据检索到subpage.php上的示例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 在您的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. 根据$ page的值,您可以选择运行哪个查询以及在页面中显示什么。

To add to all these, you don't need to have 14 tables to cater 14 links. 要添加所有这些内容,您不需要有14个表即可满足14个链接。 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....  |

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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