-2

Im running the latest version of XAMPP and its connecting to a MSSQL DATABASE with the most current drivers for PHP 7.3.8 the queries are being issued using sqlsrv_query() + sqlsrv_fetch_array()

Ok so I have a SQL Query I need to run from PHP which is:

$sql = 'SELECT distinct "Name" FROM vItem WHERE "Name" LIKE pkg%';

The above code doesn't work and does not return anything but when I just run(below):

$sql = 'SELECT distinct "Name" FROM vItem';

It returns the values from the column it literally breaks after adding "WHERE" to it.

Amira Bedhiafi
  • 8,088
  • 6
  • 24
  • 60
demo7up
  • 530
  • 5
  • 27
  • 1
    $sql = "SELECT distinct \"Name\" FROM vItem WHERE \"Name\" LIKE 'pkg%'"; – Manzolo Aug 26 '19 at 13:35
  • 1
    I've nominated this for re-opening because the "duplicate" question is about MySQL, whereas this is about SQL Server. The syntax is different. – alroc Aug 26 '19 at 13:37
  • It has nothing to do with the backticks as changing the backticks and quotes still gives the same result! – demo7up Aug 26 '19 at 13:40
  • Adding WHERE to the statement literally breaks it! ```$sql = 'SELECT distinct "Name" FROM vItem';``` works perfectly fine! – demo7up Aug 26 '19 at 13:41
  • 1
    why @JayBlanchard? @demo7up tell that `$sql = 'SELECT distinct "Name" FROM vItem';` works – Manzolo Aug 26 '19 at 13:44
  • again ```$sql = 'SELECT distinct "Name" FROM vItem';``` works with no problem its when I extend the query with "WHERE" ```$sql = 'SELECT distinct "Name" FROM vItem WHERE "Name" LIKE pkg%';``` that it stops working – demo7up Aug 26 '19 at 13:46
  • You guys are quick to assume its the backticks when its not doesnt matter which quotes, or ticks i use the result is the same! – demo7up Aug 26 '19 at 13:48
  • @AndreaManzi - ```$sql = "SELECT distinct \"Name\" FROM vItem WHERE \"Name\" LIKE 'pkg%'";``` WORKED LIKE A CHARM THANKS SO MUCH! – demo7up Aug 26 '19 at 13:52
  • The actual answer was escaping the quotes, not which backticks or quotes were used! – demo7up Aug 26 '19 at 13:54
  • 1
    The actual answer was quoting the variable `pkg&`, not escaping the quotes. – Jay Blanchard Aug 26 '19 at 13:54
  • @alroc it should be duped with https://stackoverflow.com/questions/1586560/how-do-i-escape-a-single-quote-in-sql-server then. – Jay Blanchard Aug 26 '19 at 13:55
  • OK, but what mean "Double quotes in a SQL query like you're showing is just wrong"? – Manzolo Aug 26 '19 at 13:56
  • 1
    I have deleted that comment @AndreaManzi – Jay Blanchard Aug 26 '19 at 13:56
  • @JayBlanchard the double quotes were escaped not the single quotes – demo7up Aug 26 '19 at 14:06
  • Field names are quoted by backticks, not quotes. – Markus Zeller Aug 26 '19 at 14:25
  • You have no single quotes around your variable value in your question. The vairable value must be quoted, unless it is an integer. – Jay Blanchard Aug 26 '19 at 15:20

2 Answers2

3

Try this:

 $sql = "SELECT distinct \"Name\" FROM vItem WHERE \"Name\" LIKE 'pkg%'"; 
Manzolo
  • 1,909
  • 12
  • 13
0

Try this :

  $sql = "SELECT distinct \"Name\" FROM vItem WHERE \"Name\" LIKE 'pkg%'";
Amira Bedhiafi
  • 8,088
  • 6
  • 24
  • 60