php - (PHP) 如何将数据库中两个不同表的数据显示到 Bootstrap 表

标签 php mysql twitter-bootstrap

我是 PHP 初学者。我目前正在开发一个网站,我想在 Bootstrap 中显示来自 MySQL 数据库中 2 个不同表的数据。这两个表是客户用户帐户。我想在一行中显示他们的数据。这是我现在拥有的:(Please Click to see the image)正如您在照片中看到的,我的问题是我只能显示client 表中的数据,但不能显示useraccounts 表中的数据。 Bootstrap 表中的空白单元格应该是来自useraccounts表的数据,但我未能显示它。

<小时/>

这是我的客户用户帐户表格:(tables) Please click the image

我们将非常感谢您的帮助。这是我的代码:

<?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>";
?> 

最佳答案

问题中共享的表结构没有显示两个表之间的关系。您可以使用 JOIN查询从两个表中获取数据,但是您需要在表之间建立关系才能连接它们。下面是可以使用的查询。

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   ....

在此查询中,<some field><matching field>指定有助于连接表的关系(可以是一个表的 PK 作为另一表中的 FK)。此外,您还可以附加 WHERE如果需要,可以使用子句过滤数据。

虽然这不能为您的问题提供完整的答案,但它应该可以帮助您提供适当的指导以继续前进。

关于php - (PHP) 如何将数据库中两个不同表的数据显示到 Bootstrap 表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45355730/

相关文章:

twitter-bootstrap - 将 Bootstrap 中的列内的内容居中

javascript - 制作 <li> 元素 'active' 而不是 <a> 元素

javascript - 如何更改事件选项卡文本颜色?

php - 如何在 Laravel PHP 框架中合并两个集合而不删除(丢失)键?

javascript - 当我单击图像进行评分时,另一张评分图像中也会发生同样的情况

php - 重写 URL www.domain.com 导致我的页面出现 404

mysql - 提高sql连接计数查询的性能

mysql - 如何在 Windows 上使用 Node JS 访问 MariaDB

php - 为什么 PHPUnit 隐藏我的 xdebug 回溯?

php - PHP 中的常量与属性?