PHP来自多个表,但在同一个数据库中

时间:2016-05-04 08:13:48

标签: php localhost

我的php SELECT查询工作正常但我需要它才能显示超过1个表的数据。

$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表数据的代码,但我需要几个,我已经尝试将其更改为此以查看是否会显示更多表:

$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在所有表格中)如果您对如何在其他表格中显示数据有所了解,请提供帮助!谢谢!

2 个答案:

答案 0 :(得分:1)

为多个表格中的获取数据添加了联接

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

答案 1 :(得分:0)

如果你不想使用union或join(left,right,inner,outer)语句,你不能从多个表中获取数据,如果你想从table或less字段中选择所有字段,如果你想选择指定你尝试过像@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'";