-3

I have window column in item table and I want use this query but window also mysql command. What can I do?

Query:

"SELECT pos, vnum  FROM item WHERE owner_id='".$id_account."' AND window='MALL' ORDER by pos ASC ";

Error:

Error description: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'window='MALL'' at line 3

myfull code

    include 'ayarlar.php';
    $connforfunc = new mysqli($servername, $username, $password, 'player');
    

    if ($connforfunc->connect_error) {
        die("Connection failed: " . $conn->connect_error);
      }

    $sqlforfunc ="SELECT pos, vnum  FROM item WHERE owner_id='".$id_account."' AND 'window'='MALL' ORDER by pos ASC ";

    
    if (!$connforfunc -> query($sqlforfunc)) {
        echo("Error description: " . $connforfunc -> error);
      }

    $resultforfunc = $connforfunc->query($sqlforfunc);

    while($row = $resultforfunc->fetch_assoc()) {
        echo $row['pos'];
        
        
    }
hiqermod
  • 1
  • 2
  • https://dev.mysql.com/doc/refman/8.0/en/keywords.html – CBroe Oct 22 '21 at 10:37
  • @user3783243 this is how it works https://www.db-fiddle.com/f/2aC1FwMpuv4L8fKhrrWzh8/0 but it doesn't work when I make the column name window https://www.db-fiddle.com/f/wT7JgP5riRGHhZddZyn1A1/0 – hiqermod Oct 22 '21 at 11:29
  • You are not differentiating between single quotes and backticks. Quotes are next to the `enter`/`return` key. The backtick is top left, next to the `1`. Quotes are for strings, backticks are for identifiers. – user3783243 Oct 22 '21 at 12:47

2 Answers2

0

As window is the reserve keyword you need to use `` sign in your column name.

   "SELECT pos, vnum  FROM item WHERE owner_id='".$id_account."' AND `window` ='MALL' ORDER by pos ASC ";
Musabbir Mamun
  • 251
  • 3
  • 6
  • When I do this, I do not get a query error, but I do not receive the requested information, and when I change the table name to windowome, there is no problem. – hiqermod Oct 22 '21 at 11:05
0

The solution to the problem is that we write the table name at the beginning of the column so that mysql can see the windows command as a column. Here is the solution to my problem

SELECT pos, vnum  
FROM item 
WHERE owner_id='".$id_account."' AND item.window='MALL' ORDER by pos ASC

other than that putting the column name in quotes didn't solve my problem

I was not getting the data even though I was not getting any error messages

hiqermod
  • 1
  • 2
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Oct 22 '21 at 12:44