php - How can I get an id of a left joined mySQL table? -


like mysql request working well:

 <?php        $id = 12;          $pdo = $db->prepare("         select *,         horse.id h         dog          left join cat on dog.name=cat.name         left join horse on dog.name=horse.name         animal = ?");         $pdo->execute(array($id));         $animals = $pdo->fetchall();          foreach ($animals $row) {             echo $row["h"];         }         ?> 

but if want dog.id...

 <?php          $id = 12;          $pdo = $db->prepare("         select *,         horse.id h         dog.id d         dog          left join cat on dog.name=cat.name         left join horse on dog.name=horse.name         animal = ?");         $pdo->execute(array($id));         $animals = $pdo->fetchall();          foreach ($animals $row) {             echo $row["h"];             echo $row["d"];         }         ?> 

...there error:

fatal error: uncaught pdoexception: sqlstate[42000]: syntax error or access violation: 1064 have error in sql syntax; check manual corresponds mysql server version right syntax use near 'dog.id d dog left join cat on dog.name=cat.name' @ line 3 in /myproject.php:31 stack trace: #0 /myproject.php(31): pdostatement->execute(array) #1 {main} thrown in /myproject.php on line 31

based on data , error message, missed 1 comma in query after horse.id h should be

select *,  horse.id h,  dog.id d dog  left join cat on dog.name=cat.name left join horse on dog.name=horse.name 

Comments

Popular posts from this blog

inversion of control - Autofac named registration constructor injection -

verilog - Systemverilog dynamic casting issues -

ios - Change Storyboard View using Seague -