0

Yes, now I know about JOIN, but still didn't solve this problem.

$gender = 6;

$result = mysqli_query($con,"SELECT * FROM ps_customer c LEFT JOIN ps_adress a ON a.customer_id=c.customer_id WHERE c.id_gender='$gender'");

var_dump($result);

mysqli_close($con);

This is my code, but result: false

I tried var_dump to check why I cant print my data.

Is this data print correct for my code?

 echo "<h1>People</h1>";
echo "<table border='1'>
<tr>
<th>ID</th>
<th>Name</th>
<th>Lastname</th>
<th>Email</th>
<th>Company</th>
<th>Phone</th>
</tr>";



$result = mysql_query($query) or die(mysql_error());
echo var_dump($result);
while($row = mysql_fetch_array($result)){
  echo "<tr>";
  echo "<td>" . $row['id_customer'] . "</td>";
  echo "<td>" . $row['firstname'] . "</td>";
  echo "<td>" . $row['lastname'] . "</td>";
  echo "<td>" . $row['email'] . "</td>";
  echo "<td>" . $row['company'] . "</td>";
  echo "<td>" . $row['mobile_phone'] . "</td>";
  echo "</tr>";
}
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129

4 Answers4

0

Simple join will work.

SELECT *
FROM ps_customer c
    LEFT JOIN ps_address a ON a.customer_id=c.customer_id
WHERE c.id_gender = '$gender'
DarkSide
  • 3,670
  • 1
  • 26
  • 34
  • Okey, can u help me how to print all data? While not working mh... Dk Im using your example. –  Jun 11 '14 at 12:40
  • Didn't get what you want, but you should really start by learning HTML, PHP and MySQL basics. – DarkSide Jun 11 '14 at 16:49
0
SELECT *
FROM adress a
INNER JOIN customer c ON a.customer_id = c.id
WHERE c.id_gender = $gender
Sadikhasan
  • 18,365
  • 21
  • 80
  • 122
Kaiser
  • 748
  • 7
  • 21
0

You need a LEFT JOIN (in case of customers having no adress record). Asuming both tables has the same column name "customer_id" to identify the customer, you can use:

SELECT ps_customer.name,
       ps_customer.lastname,
       ps_customer.company,
       ps_address.phone,
       ps_address.mobile
FROM ps_customer
LEFT JOIN ps_address ON ps_address.customer_id = ps_customer.customer_id
WHERE ps_customer.id_gender = '$gender'

But be carefully here. If there are multiple adress-records per one customer possible, you'll get multiple rows containing the same customer data for each adress record. If this is the case, you need a more sophisticated query :)

Sadikhasan
  • 18,365
  • 21
  • 80
  • 122
itinance
  • 11,711
  • 7
  • 58
  • 98
0

Your query should be

"select cus.name, cus.lastname, cus.company from ps_customer cus, ps_address add where id_gender='".$gender."' and cus.customer_id=add.customer_id"
Sam
  • 41
  • 2
  • 13