[英](PHP) How to display data from 2 different tables in the database to a bootstrap table
I'm a beginner in PHP. 我是PHP的初学者。 I'm currently developing a website where I want to display in the bootstrap table
the data that will come from 2 different tables in the MySQL database. 我目前正在开发一个网站,该网站要在引导table
显示来自MySQL数据库中2个不同表的数据。 The two tables are client and useraccounts . 这两个表是client和useraccounts 。 I want to display their data in a single row. 我想在一行中显示其数据。 Here is what I have right now: (Please Click to see the image) As you can see on the photo, my problem is I can only display the data from the client table but not from the useraccounts table . 这是我现在所拥有的:( 请单击以查看图片)如您在照片上所看到的,我的问题是我只能显示来自客户端表的数据,而不能显示来自useraccounts表的数据 。 The blank cells in the bootstrap table should be the data coming from the useraccounts table but I failed to display it. 引导程序表中的空白单元格应该是来自useraccounts表的数据,但我无法显示它。
Here is my table for client and useraccounts: (tables) Please click the image 这是我的客户和用户帐户 表格 :( 表格)请点击图片
Your help will be highly appreciated. 非常感谢您的帮助。 Here is my code: 这是我的代码:
<?php
echo "<table style='cursor: pointer;' class='table table-hover table-striped
table-bordered table-responsive ' >";
echo "<tr>
<th>Company Name</th>
<th>Contact Person</th>
<th>Client Address</th>
<th>Contact no.</th>
<th>E-mail</th>
<th>User Birthdate</th>
<th>User Mobile no.</th>
<th>Username</th>
<th>User Fistname</th>
<th>User Lastname</th>
<th>User Birthdate</th>
</tr>";
class TableRows extends RecursiveIteratorIterator {
function __construct($it) {
parent::__construct($it, self::LEAVES_ONLY);
}
function current() {
return "<td>" . parent::current(). "</td>";
}
function beginChildren() {
echo "<tr class='table table-hover'>";
}
function endChildren() {
echo "</tr>" . "\n";
}
}
$servername = "mysql5013.hostbuddy.com";
$username = "*****_crb";
$password = "*****";
$dbname = "db_*****_crb";
try {
//data from client table
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username,
$password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $conn->prepare("SELECT cli_company_name, cli_contact_person,
cli_address, cli_contactno, cli_email FROM client WHERE status=1 ");
$stmt->execute();
// set the resulting array to associative
$result = $stmt->setFetchMode(PDO::FETCH_ASSOC);
//data from useraccounts table
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt2 = $conn->prepare("SELECT username, user_fname, user_lname, user_bdate
FROM useraccounts WHERE status=1 AND
company_name='".$result['cli_company_name']."' ");
$stmt2->execute();
// set the resulting array to associative
$result2 = $stmt2->setFetchMode(PDO::FETCH_ASSOC);
foreach(new TableRows(new RecursiveArrayIterator($stmt->fetchAll())) as
$k=>$v) {
echo $v;
}
foreach(new TableRows(new RecursiveArrayIterator($stmt2->fetchAll())) as
$l=>$w) {
echo $w;
}
}
catch(PDOException $e) {
echo "Error: " . $e->getMessage();
}
$conn = null;
echo "</table>";
?>
The table structure shared in the question do not show a relationship between the 2 tables. 问题中共享的表结构未显示2个表之间的关系。 You can use a JOIN
query to fetch the data from the 2 tables however you will need to have a relation between the tables in order to join them. 您可以使用JOIN
查询从2个表中获取数据,但是您需要在表之间建立关系才能连接它们。 Below is the query that can be used. 下面是可以使用的查询。
select c.cli_company_name,
c.cli_contact_person,
c.cli_address,
c.cli_contact_no,
c.cli_email,
ua.user_bdate,
ua.user_mobilenum,
ua.username,
ua.user_fname,
ua.user_lname
from client c
join useraccounts ua
on c.<some field> = ua.<matching field>
where ....
In this query, the <some field>
and <matching field>
specify the relation (it could be PK of one table as FK in the other table) that will help in joining the tables. 在此查询中, <some field>
和<matching field>
指定有助于连接表的关系(它可以是一个表的PK,另一表的FK)。 Additionally you can also append a WHERE
clause to filter the data if required. 另外,如果需要,您还可以附加WHERE
子句以过滤数据。
Though this would not provide a complete answer to your question, it should help you in providing appropriate guidance to move ahead. 尽管这不能完全回答您的问题,但它应该可以帮助您提供适当的指导以继续前进。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.