-3

I have store the same data in two tables: tableA and tableB. I want to select first from tableA, and if there is no data, I want to used tableB. tableA and tableB hold exactly the same data. I used tableB for archiving. There maybe times when we delete data from tableA but it will still be there in tableB.

$q = $dbh->query("SELECT * FROM tableA WHERE id='1'");
if($q->fetchColumn==NULL){
    $q = $dbh->query("SELECT * FROM tableB WHERE id='1'");
}

foreach($q->fetchAll() as $data):
     echo $data['name'];
endforeach;

The problem with this code is that it returns from tableB whereas tableA data is there. How to solve this problem? Please help.

TableA

     id, name, age
     1   john   45

TableB

   bid, id,   name,    age
    1    1  old john    45

If there is no data in tableA, I want the query to select from tableB. But if there is data in TableA, I want to select from it(tableA). My code outputss from tableB.

The errors:

   fetchColumn()==NULL//does not output anything
   fetchColumn(0)==NULL//does not output anything
   fetchColumn==NULL//outputs from tableB
   fetchColumn==0//outputs from tableB
   fetchColumn()==0/does not output anything

Expected output: john

PHP Version 7.1.1

3 Answers3

0

This will work, have tested with the sample data you given, as Jay have said when u use fetchColumn() you need to specify atleast one column.

<?php
    ini_set('display_errors', 1);
    error_reporting(E_ALL);

    $q    = $dbh->query("SELECT * FROM tablea WHERE id='1'");
    $data = $q->fetchColumn(1); //name
    if ($data) {

        echo $data;

    } else {

        $q = $dbh->query("SELECT * FROM tableb WHERE id='1'")->fetchall();
        foreach ($q as $data) {

            echo $data['name'];
        }

    }

    ?>

Results :

enter image description here

Results when id from table a is not 1

enter image description here

Masivuye Cokile
  • 4,754
  • 3
  • 19
  • 34
0

Fantastic question, fantastic answers.

If there is no data in tableA, I want the query to select from tableB. But if there is data in TableA, I want to select from it(tableA).

$stmt = $dbh->prepare("SELECT * FROM tableA WHERE id=?");
$stmt->execute([$id]);
$data = $stmt->fetch();
if (!$data) {
    $stmt = $dbh->prepare("SELECT * FROM tableB WHERE id=?");
    $stmt->execute([$id]);
    $data = $stmt->fetch();
}
echo $data['name'];

incredible complex logic with a lot of queries

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
  • 2
    So, answers which work get downvoted? I don't understand, please enlighten. It is one thing to disagree with method, quite another to DV just because you don't care for the complexity. – Jay Blanchard Mar 20 '17 at 16:23
-2

Million thanks to everyone who helps me here. I solved my problem by adding another query to check the first query. This solution gives me what I want.

$id = 1;
$qr = $dbh->prepare("SELECT id, name, age FROM tableA WHERE id=:id");
$qr->execute(array(':id' => $id));
$res = $dbh->prepare("SELECT COUNT(*) FROM tableA WHERE id=:id");   
$res->execute(array(':id' => $id));
$row = $res->fetchColumn();
if( $row <= 0){ 
   unset($qr);
   $qr = $dbh->prepare("SELECT * FROM tableB WHERE id=:id");
   $qr->execute(array(':id' => $id));
}
$results = $qr->fetchAll();
foreach($results as $data):
     echo $data['name'];//output is john
endforeach;
  • this is not how u use pdo – Masivuye Cokile Mar 20 '17 at 16:13
  • Yeah -- you're covering up for some other issue somewhere. There is *no reason* you should have to use another query. – Jay Blanchard Mar 20 '17 at 16:17
  • [Little Bobby](http://bobby-tables.com/) says ***[your script is at risk for SQL Injection Attacks.](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php)***. Even [escaping the string](http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string) is not safe! – Jay Blanchard Mar 20 '17 at 16:18
  • Yes I know. I use prepare query in my real code. This here is for my solution in an easy to understand code. – Zone Security Mar 20 '17 at 16:47
  • @JayBlanchard, I fixed my code with prepared query. This answer is which works for me. Without using another query as you suggested, It did not work for me. – Zone Security Mar 20 '17 at 17:16