0

Please Help

i am getting

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax

while attempting to run the query below, i know the query have one infect couple of syntax errors which i don't know how to solve

$myQuery = mysql_query("SELECT key
                        from DE_user_stats
                        WHERE article_id = 671
                        AND domain = 2nd.com/
                        AND userid = 2") 
                or die($myQuery."<br/><br/>".mysql_error());

The table look like this :

key - article_id - domain - userid 13 671 2nd.com/ 2

The output should be 13.

Thanks

( i am not using mysql and no one should it was just for an experiment, please don't hate me)

Hifi
  • 17
  • 5
  • 2
    non numeric value should be quoted . – Niklesh Raut Jan 19 '17 at 13:00
  • The error message goes no to try and indicate __Where the error is___ Did you look at that bit???? – RiggsFolly Jan 19 '17 at 13:01
  • Every time you use [the `mysql_`](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) database extension in new code **[a Kitten is strangled somewhere in the world](http://2.bp.blogspot.com/-zCT6jizimfI/UjJ5UTb_BeI/AAAAAAAACgg/AS6XCd6aNdg/s1600/luna_getting_strangled.jpg)** it is deprecated and has been for years and is gone for ever in PHP7. If you are just learning PHP, spend your energies learning the `PDO` or `mysqli` database extensions and prepared statements. [Start here](http://php.net/manual/en/book.pdo.php) – RiggsFolly Jan 19 '17 at 13:02
  • @RiggsFolly , just learning... once get the mysql one working will move to mysqli – Hifi Jan 19 '17 at 13:05
  • @rishi, if quote "2nd.com/" get this error : Parse error: syntax error, unexpected '2' (T_LNUMBER) – Hifi Jan 19 '17 at 13:06
  • did you try as Gordon said ? – Niklesh Raut Jan 19 '17 at 13:08
  • @Naby Since your query string is already delimited by double quotes (`"`), you have to escape your value with single quotes (`'`). And may I add, you should already start using `mysqli`, regardless of how long you've been learning. – roberto06 Jan 19 '17 at 13:11
  • 2
    _once get the mysql one working will move to mysqli_ **Why waste your time learning mysql_*** – RiggsFolly Jan 19 '17 at 13:11
  • @RiggsFolly i am not learning mysql at all its just this query which have multiple where conditions which i was unable to make with PDO and then tried with mysql and was unable to make with mysql as well so posted here, no reason to hate this was just for an idea and i get it from the answer below. thanks anyways – Hifi Jan 19 '17 at 13:17
  • _was unable to make with PDO_ Its not PDO's fault if you code a query that will not compile or run. Go back to using PDO with the fixed query and it will work – RiggsFolly Jan 19 '17 at 13:20
  • @RiggsFolly actually i did posted the same question with PDO query couple of days back : http://stackoverflow.com/questions/41703154/selecting-where-multiple-conditions-match-safemysql but still could not get it working so thought lets 1st try with mysql if it works, that's all :) PS if you can help with the PDO one ? – Hifi Jan 19 '17 at 13:24

2 Answers2

2

You have multiple errors. key is a reserved word and needs to be escaped. And, you need proper quotes around string constants:

SELECT `key`
FROM DE_user_stats
WHERE article_id = 671 AND
      domain = '2nd.com/' AND
      userid = 2;

I would strongly advise you to change the name of the key column, so it does not need to be escaped.

Gordon Linoff
  • 1,198,228
  • 53
  • 572
  • 709
  • Worked, thank you so much, while try to echo the key with $valuess = mysql_fetch_array($myQuery); var_dump($valuess); getting the result like this array(2) { [0]=> string(6) "rKSpxf" ["keyy"]=> string(6) "rKSpxf" } how can i just get the "rKSpxf" as output which is the key. – Hifi Jan 19 '17 at 13:20
  • Use the correct fetch function! Like `mysql_fetch_assoc()` – RiggsFolly Jan 19 '17 at 13:23
  • Or if you really were using PDO `$result->fetch_assoc()` – RiggsFolly Jan 19 '17 at 13:23
  • @RiggsFolly with mysql_fetch_assoc() getting output like : array(1) { ["keyy"]=> string(6) "rKSpxf" } i am looking for only to echo rKSpxf – Hifi Jan 19 '17 at 13:30
  • Oh lord.... An array ALWAYS has a key. In a result from a query the key is always the COLUMN NAME. If you want the value then you do `$var = $result['key']` – RiggsFolly Jan 19 '17 at 13:34
0

Just add Quotes, it work

$myQuery = mysql_query("SELECT key 
                        from DE_user_stats  
                        WHERE article_id = 671  
                        AND domain = '2nd.com'  
                        AND userid = 2") 
                or die($myQuery."<br/><br/>".mysql_error());
RiggsFolly
  • 89,708
  • 20
  • 100
  • 143
amit 1984
  • 414
  • 3
  • 9