I want to display the names by matching the value of att_card_no in the ent
table to cardno in the att_user table.
try {
$att = $att->query('select att_time,att_date,att_card_no from att_detail WHERE att_card_no IN (select name from att_user where cardno="2342323423")');
$att -> execute();
$result = $att -> fetch();
}
catch (PDOException $e){
echo $att . "<BR>" . $e->getMessage();
}
$att -> execute();
?>
<?php echo "Name: ".$result['att_card_no']; ?>
Table:att_detail
+---------+----------+-------------+------------+
| no | att_time | att_date | att_card_no|
+---------+----------+-------------+------------+
| 1 | 08:01 | 11-12-17 | 2342323423 |
| 2 | 08:02 | 12-12-17 | 4574574577 |
| 3 | 08:03 | 13-12-17 | 4656456796 |
| 4 | 08:04 | 14-12-17 | 9343450984 |
+---------+----------+-------------+------------+
Table: att_user
+----+-------------+-------------+
| no | name | cardno |
+----+-------------+-------------+
| 1 | name1 | 2342323423 |
| 2 | name2 | 4574574577 |
| 3 | name3 | 4656456796 |
| 4 | name4 | 9343450984 |
+----+-------------+-------------+
But the name value from att_user is not displayed. Appreciate in understanding what is wrong here. There are no errors throwing up too.
Edit: If you cannot answer it or if you find a similar Q that solves my problem please let me know because my searches did not find any but down voting this question is not helping a bit
There are several problems with your SQL statement.
Since name isn't in the select's list of columns, it won't be in the result set. In your where, you are trying to match on att_card_no = name which won't ever match.
Here's your SQL statement:
select
att_time,
att_date,
att_card_no
from att_detail
WHERE att_card_no IN (select name from att_user where cardno="2342323423")
But this is what you really want:
select
b.`name`,
a.`att_time`,
a.`att_date`,
a.`att_card_no`
from ent a
JOIN att_user b
ON a.`att_card_no` = b.`cardno`;
You need to use JOIN
query like below:-
(SELECT att_user.name,att_detail.att_time,att_detail.att_date,att_detail.att_card_no FROM att_user LEFT JOIN att_detail on att_user.cardno = att_detail.att_card_no WHERE att_user.cardno="2342323423");
Note:-
You query is actually converted like this:-
'select att_time,att_date,att_card_no from att_detail WHERE att_card_no IN ("name1")'
So basically no name
column is selected there, and hens it not coming.
you should use a JOIN
$att = $att->query('
select a.att_time
, a.att_date
, a.att_card_no
, b.name
from att_detail as a
inner join att_user as b on a.att_card_no = b.card_no and b.cardno="2342323423"'
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.