I am trying to do what I just said. I have two joined tables. They are joined by same id's. But There are two columns with same name and I only want to output one of the columns.
$query= "SELECT *
FROM users
INNER JOIN bookings ON users.username = bookings.personal
JOIN products ON bookings.productid = products.productid
WHERE users.id = $id
";
$selectproductname = $db->prepare($selectuseremailquery);
$selectproductname ->execute(array());
foreach($selectproductname as $index => $rs) :
it gives me something like this table:
-id -----id----products----products------
- -
-1 1 a cup coffee cup -
-2 2 a pen a purple pen -
-----------------------------------------
I want to output the coffee cup, but there are two columns with same name. How can I output?
This didn't work:
<td><div> <?php echo $rs['products.productname']; ?> </div> </td>
And I have to select all of it with *.
Using SELECT *
is generally considered harmful in production software, especially in php, for the precise reason you're asking about.
In php, sometimes the result set is loaded into an associative array. That will cause data from all but one of each set of duplicate column names to disappear.
You want to use
SELECT products.id,
products.productname,
products.id AS product_id,
bookings.*,
etc., etc.
to enumerate the columns you need in your result set. (Notice that I'm guessing at your column names).
I know your question says you have to use SELECT *
. I doubt that's true. If so it's a likely to be requirement imposed by somebody who doesn't know what they're talking about. (Stupid professor tricks come to mind.)
If you do have to use SELECT *
, you'll need to use the result set metadata to examine each column's metadata and figure out which columns you need to extract. getColumnMeta()
does that.
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.