0

im trying to get two separate columns of a database to CONCAT together in a check.

What i currently have is

<?php

$retrieve = "SELECT * FROM `Dogs` WHERE CONCAT(`Date`, 
`Time`) =". $_GET["id"];

$result = $conn->query($retrieve);
while($row = $result->fetch_assoc()) {

  echo $row['Name'] . $row['Date'] . 
$row['Time'];

}
?>

However, it doesn't seem to be compiling, and im stumped as to where i've gone wrong with it, as it should be merging the two together? I know the Get is working as i have an echo printing it out, and does so correctly.

Any guidance is helpful

The error i receive, is

You have an error in your SQL syntax; check the manual that corresponds to 
your MariaDB server version for the right syntax to use near ':00:00' at 
line 1
Joe
  • 1
  • 1
  • Does this answer your question? [When to use single quotes, double quotes, and backticks in MySQL](https://stackoverflow.com/questions/11321491/when-to-use-single-quotes-double-quotes-and-backticks-in-mysql) – sticky bit Dec 08 '19 at 20:11
  • 1
    Also: You should use parameterized queries. Read: [How can I prevent SQL injection in PHP?"](https://stackoverflow.com/search?q=php+sql+injection) – sticky bit Dec 08 '19 at 20:12
  • @stickybit hi, thanks for your response. As far as i can tell, i'm using the appropriate quotes? And i plan o ntackling parameterized queries after i get the actual queries working – Joe Dec 08 '19 at 20:17
  • So `$_GET["id"]` is `'DateTime'`? – sticky bit Dec 08 '19 at 20:18
  • @stickybit `$_GET["id]` is `Date+Time` as they're two seperate columns int he database. So the `$_GET["id]` is a string of the two merged together in a singular string, not added together mathematically – Joe Dec 08 '19 at 20:21
  • Your not concatenating columns but literals. Your quotes are wrong for what you want to do. – sticky bit Dec 08 '19 at 20:27
  • Side note: Don't store date and time as (two) `integers`. Use an appropriate data type like `datetime`. – sticky bit Dec 08 '19 at 20:30
  • @stickybit `"SELECT * FROM ``performance`` WHERE CONCAT(``PerfDate`` ``PerfTime``) =". $_GET["id"];` i've tried using these quote's for it too, and still an error. ignore the fact they're double quoted here, was the only was i could format it. In my code its quoted singular – Joe Dec 08 '19 at 20:33
  • There's no comma between the arguments to `concat()`... – sticky bit Dec 08 '19 at 20:34
  • @stickybit oh sorry i think i actually somehow deleted it from my comment, my code does have a comma in between the arguments and still doesn't work – Joe Dec 08 '19 at 20:37
  • @stickybit i updated the post with the correct quotes and the error message – Joe Dec 08 '19 at 20:46
  • Again: Use an appropriate data type and parameterized queries. – sticky bit Dec 08 '19 at 20:49
  • @stickybit i'm unfortunately unable to change the data type of the database, and i'm going to parameterize the query once it's working. So in short im unable to change it to DateTime and have to merge the two strings together – Joe Dec 08 '19 at 20:54

0 Answers0