MYSQL to SQL – Limit in Update

limitmysqlsql

I try to change my code from MYSQL to SQL and i got an error (SQL Syntax 'Limit').

So i tried to change my query and update with "TOP" but seems to work only with SELECT.

So, how can i change this MYSQL query :

$fct="UPDATE `users` SET `STREAM_TITRE` = '$STREAM_TITRE',`STREAM_URL` = '$STREAM_URL',`STREAM_DESC` = '$STREAM_DESC',`STREAM_GENRE` = '$STREAM_GENRE' WHERE `ID` =$IDSESS LIMIT 1";

Here is my SQL Code without Limit :

$fct="UPDATE users SET STREAM_TITRE = '$STREAM_TITRE', STREAM_URL = '$STREAM_URL', STREAM_DESC = '$STREAM_DESC', STREAM_GENRE = '$STREAM_GENRE' WHERE ID = '$IDSESS'";

Thanks

Best Solution

It's not very clear which version of your query is working and which is not - and in what DBMS.

If ID is of char or varchar type, you are missing some quotes in the LIMIT version. Although MySQL is not very picky and you won't have many issues, with or without quotes:

$fct = "
  UPDATE users 
  SET STREAM_TITRE = '$STREAM_TITRE'
    , STREAM_URL = '$STREAM_URL'
    , STREAM_DESC = '$STREAM_DESC'
    , STREAM_GENRE = '$STREAM_GENRE' 
  WHERE ID = $IDSESS              --<-- this should be '$IDSESS' , right?
                                  ----- or $IDSESS , depending on the datatype
  LIMIT 1
       ";

Note: The LIMIT n works in MySQL and PostgreSQL, but not in some other DBMS. Plus, I don't think you really need it anyway, as the ID is probably the Primary Key of the table.

If you are trying to convert the statement from MySQL to SQL-Server, you should not use the backquotes and replace LIMIT 1 with TOP (1):

$fct = "
  UPDATE TOP (1) users 
  SET STREAM_TITRE = '$STREAM_TITRE'
    , STREAM_URL = '$STREAM_URL'
    , STREAM_DESC = '$STREAM_DESC'
    , STREAM_GENRE = '$STREAM_GENRE' 
  WHERE ID = $IDSESS             
       ";