[英]How to get information from 2 tables at once in PHP and MySQL?
<?php
include('includes/config.php');
$topi = $_GET['id']; //id of url
mysql_select_db("ban", $con);
$query = "SELECT * FROM `basic` WHERE id = '$topi' LIMIT 0, 30";
$result = mysql_query($query) or die(mysql_error());
$row = mysql_fetch_array($result) or die(mysql_error());
$aa = $row['item'];
$cc = $row['moreinfo'];
$dd = $row['contactinfo'];
$ff = $row['id'];
In this script, I get information from the table basic
, but I want to retrieve data from another table named users
.在这个脚本中,我从表
basic
中获取信息,但我想从另一个名为users
的表中检索数据。 How can I retrieve data from two tables at once?如何一次从两个表中检索数据?
users
table consists of following columns: users
表由以下列组成:
You need to JOIN
the two tables on a common value, called aforeign key .您需要将
JOIN
表连接到一个共同的值上,称为外键。 Once you've posted the structure of the users
table as requested in the comments, I can provide a more complete example.一旦您按照评论中的要求发布了
users
表的结构,我可以提供一个更完整的示例。
EDIT : See example.编辑:见例子。 This calls explicit column names instead of
SELECT *
.这会调用显式列名而不是
SELECT *
。
$query = "SELECT
basic.id,
basic.item,
basic.moreinfo,
basic.contactinfo,
users.email,
users.username
FROM basic JOIN users ON basic.id = users.id
WHERE id = '$topi'
LIMIT 0 , 30";
You would use a JOIN onto the other table.您将在另一个表上使用 JOIN。
$query = "SELECT *
FROM basic b
JOIN users u ON b.user_id = u.user_id
WHERE id = '$topi'
LIMIT 0, 30";
Something like that, but based on your fields.类似的东西,但基于你的领域。
Please Note: the ON clause specifies what you will be looking for a match on.请注意:ON 子句指定您要查找的匹配项。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.