0

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

4
  • I doubt that SQL Syntax 'Limit' is your complete error? And what is the problem? It seems to me you have a query that doesn't work, and have one that does, so what is the problem is you have one that works? Commented Apr 10, 2012 at 10:05
  • 1
    What does the query look like if you print $fct? Commented Apr 10, 2012 at 10:10
  • What version of SQL Server are you using? The documentation I've seen states that you can use TOP with UPDATE. Does it simply not work, or do you get an error message? Commented Apr 10, 2012 at 10:18
  • 1
    What do you mean with "to SQL"? SQL is a language that is also used by MySQL. Commented Apr 10, 2012 at 12:15

1 Answer 1

5

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             
       ";
Sign up to request clarification or add additional context in comments.

2 Comments

Changed it thanks. If you find a SQL Version, it would be perfect but thanks ;)
What do you mean with "SQL version"? Microsoft SQL-Server?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.