如何从2个mysql表中获取数据

时间:2014-06-11 14:03:52

标签: mysql get

这是我的CODE,我需要来自2个表的数据。第一个:ps_customer(id_customer,firstname,lastname,email)和第二个:ps_adress(phone_mobile)

我收到了错误:

  

警告:mysql_query()期望参数2是资源,第19行/home/domain/public_html/k2.php中给出的对象

这是我的代码:

<?php

$conn=mysqli_connect("localhost","login","pass","dbNAME");

if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$gender = 6;

$sql = 
    "SELECT 
         ps_customer.id_customer, ps_customer.firstname, ps_customer.lastname, ps_customer.email, ps_adress.phone_mobile
    FROM 
       ps_customer, ps_adress
    ps_customer.id_gender = '$gender' ";

    $rs=mysql_query($sql,$conn) or die(mysql_error());
    echo '<table width="100%" border="0" cellspacing="5" cellpadding="5">';
    while($result=mysql_fetch_array($rs))
    {
        echo '<tr>
                <td>'.$result["id_customer"].'</td>
                <td>'.$result["firstname"].'</td>
            <td>'.$result["lastname"].'</td>
                <td>'.$result["email"].'</td>
                <td>'.$result["phone_mobile"].'</td>
              </tr>';
    }
    echo '</table>';
    ?>

1 个答案:

答案 0 :(得分:1)

首先,你必须在两个表之间建立一个关系,比如有一个关键字段,第二个你应该加入表,然后你可以继续你的SELECT查询。