PHP 来自多个表但在同一个数据库中的结果

PHP results from multiple tables but in the same database

我的 php SELECT 查询正常工作,但我需要它显示超过 1 table 的数据。

$sql = "SELECT * from paymentPersonal where `custID`='$custID'";

$result = $conn->query($sql);

if ($result->num_rows > 0) {
     // output data of each row
     while($row = $result->fetch_assoc()) {
         echo "<br> custID: ". $row["custID"]. " - FirstName: ". $row["firstname"]. " LastName" . $row["lastname"] .  " - Mobile: ". $row["mobile"]. " - homephone: ". $row["homephone"]. " - Email: ". $row["email"]."<br>";

这是显示 1 table 数据的代码,但我需要几个,我尝试将其更改为这个代码,看看它是否会显示更多 tables:

$sql = "SELECT * from `paymentPersonal`, `paymentsPayment` where `custID`='$custID'";

    $result = $conn->query($sql);

    if ($result->num_rows > 0) {
         // output data of each row
         while($row = $result->fetch_assoc()) {
             echo "<br> custID: ". $row["custID"]. " - FirstName: ". $row["firstname"]. " LastName" . $row["lastname"] .  " - Mobile: ". $row["mobile"]. " - homephone: ". $row["homephone"]. " - Email: ". $row["email"]."<br>";

             echo "<br> custID: ". $row["custID"]. " - CcName: ". $row["nameoncard"]. " CcNumber" . $row["ccnumber"] .  " - ccYear: ". $row["year"]. " - ccMonth: ". $row["month"]. " - ccCode: ". $row["code"]."<br>";

但运气不好,只是收到一条错误消息;

Notice: Trying to get property of non-object in /Applications/XAMPP/xamppfiles/htdocs/horizonphotography/findingcode.php on line 19
0 results

(仅供参考,custID 在所有 table 中)如果您对如何在其他 table 上显示数据有想法,请帮忙!谢谢!

添加了从多个表获取数据的连接

$sql = "SELECT paymentPersonal.custID as paymentPersonalCust,paymentsPayment.custID as paymentsPaymentCust from paymentPersonal Left join paymentsPayment on paymentsPayment.custID = paymentPersonal.custID where paymentsPayment.custID='$custID'";

更改数组的键

while($row = $result->fetch_assoc()) {
         echo "<br> custID: ". $row["paymentPersonalCust"]. " - FirstName: ". $row["firstname"]. " LastName" . $row["lastname"] .  " - Mobile: ". $row["mobile"]. " - homephone: ". $row["homephone"]. " - Email: ". $row["email"]."<br>";

         echo "<br> custID: ". $row["paymentsPaymentCust"]. " - CcName: ". $row["nameoncard"]. " CcNumber" . $row["ccnumber"] .  " - ccYear: ". $row["year"]. " - ccMonth: ". $row["month"]. " - ccCode: ". $row["code"]."<br>";

如果不使用 union 或 join (left , right , inner , outer ) 语句,你不能从超过 table 中获取数据,如果你想 select 从两个 table 或更少的字段,如果你想 select 指定的字段,你可以尝试像@manindepreet singh 在第三个

中告诉你的东西
   SELECT paymentPersonal.custID as paymentPersonalCust,paymentsPayment.custID as paymentsPaymentCust from paymentPersonal Left join paymentsPayment on paymentsPayment.custID = paymentPersonal.custID where paymentsPayment.custID='$custID'";