-2

This is my code:

<?php
$stmt = $pdo->prepare("
    SELECT valueA, 'TEXT' AS valueAA FROM tableA
    UNION ALL
    SELECT valueB, 'TEXT2' FROM tableB;
");
$stmt->execute();
$points = $stmt->fetchAll();

foreach($rows as $row):
$valueA = htmlentities($row['valueA']);
$valueAVAR = number_format($valueA);
?>
<div><?php echo $valueAA . $valueAVAR; ?></div>
<?php endforeach; ?>

This is the unwanted result I am getting:

valueA
valueB

This is the result I want:

TEXTvalueA
TEXT2valueB

So basically, my code is not echoing out the word TEXT before valueA and valueB as it should based on my SQL statement. Can you please help me rewrite my code so TEXT can get echoed out? Thank you

EDIT: Sample of tableA:

valAid (int11) | valueA (int11)
  1            | 100

tableB:

valBid (int11) | valueB (int11)
  2            | 200
misner3456
  • 404
  • 2
  • 13
  • 1
    why wouldn't you add "TEXT" in the php instead of the query? its the more logical approach –  Aug 27 '19 at 02:11
  • @tim ok I guess I missed something in my question. I edited the question, but they are different words being echoed out depending on values from the database. Luckily ChrisFNZ resolved this issue. – misner3456 Aug 27 '19 at 02:25
  • 2
    i would still be doing that in the php, its not logical to use the db for that. –  Aug 27 '19 at 02:27
  • @tim Hm.. I didn't think about that. I'll definitely take your advice and consider rewriting my code. It's because that I had tried the text rendering part with php alone, but code would not work out since I was using LEFT/INNER JOINS which resulted in duplicating all my rows (an unwanted result). So someone suggested using UNION ALL, which resolved that issue. But I'm reconsidering finding a fix with using just php. Thanks for the good advice – misner3456 Aug 27 '19 at 02:58
  • The best advise you can get is to quit this union stuff and have just a single table – Your Common Sense Aug 27 '19 at 04:46

3 Answers3

0

I was in the process of writing this and saw Tim's comment, which is exactly what I was going to say. It seems much easier to add the string "TEXT" to the information you want in the PHP instead of the SQL. Here is an example:

echo "TEXT" . $valueAA . $valueAVAR;
Aaron Meese
  • 1,670
  • 3
  • 22
  • 32
  • yes it does, but I reedited my question because I missed something. The word "TEXT" is always a different value depending on what it is on the database. But ChrisFNZ resolved this issue, thanks too! – misner3456 Aug 27 '19 at 02:27
0

If you want a SELECT UNION to work then you'll need to ensure that the column names (aliases) are the same for both rows.

So instead of:

SELECT valueA, 'TEXT' AS valueAA FROM tableA
UNION ALL
SELECT valueB, 'TEXT' FROM tableB;

Would this give you closer to what you want?

SELECT valueA AS valFirst, 'TEXT' AS valSecond FROM tableA
UNION ALL
SELECT valueB AS valFirst, 'TEXT' AS valSecond FROM tableB;
ChrisFNZ
  • 597
  • 1
  • 4
  • 21
  • YES! You saved me big time! Yours is the solution I was looking for - completely fixed my code. Seriously, thank you so much! – misner3456 Aug 27 '19 at 02:24
  • 1
    You're welcome! I'm stoked that my advice was useful. – ChrisFNZ Aug 27 '19 at 02:25
  • 1
    Even a tiny bit of help can save a person's life! I am forever grateful to the people of this community. Not even in real life are people this nice enough to offer advice! – misner3456 Aug 27 '19 at 02:30
0

You never read your field valueAA from your query.

<?php
...
foreach($rows as $row):
$valueA = htmlentities($row['valueA']);
$valueAVAR = number_format($valueA);
# read valueAA
$valueAA = htmlentities($row['valueAA']);
...
?>
<div><?php echo $valueAA . $valueAVAR; ?></div>
Olivier Depriester
  • 1,615
  • 1
  • 7
  • 20
  • @OliverDepriester oh wow this was the solution too. I can't believe I completely missed that valueAA wasn't even echoed out as a row. Assumed that since it was a made-up one, it didn't need to be echoed out. But you cleared things up on the explanation part - thank you! – misner3456 Aug 27 '19 at 02:38
  • 2
    Great, because I was surprised @ChrisFNZ's answer was the solution as, as far as I know, UNION ALL does not care about the aliases. So I think your query could remain the same. – Olivier Depriester Aug 27 '19 at 02:41