MySQL 8 Query Prob With Window Keyword

109 Views Asked by At

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'];
        
        
    }
2

There are 2 best solutions below

1
Musabbir Mamun On

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 ";
1
hiqermod On

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