1

Here is my query woking absolutely fine when entered directly:

SET @new_sno=0; 
SELECT(@new_sno:=@new_sno+1) AS sno_new,Products,Price,
    case sno WHEN 1 THEN 1 WHEN 2 THEN 2 WHEN 3 THEN 3 WHEN 4 THEN 4 
             WHEN 12 THEN 5 
            ELSE 6 
    END AS orderpriority 
FROM   rates ORDER BY orderpriority,sno;

but when I use it in php like

$result = mysqli_query($con,"SET @new_sno=0; 
    SELECT(@new_sno:=@new_sno+1) AS sno_new,Products,Price,
      case sno WHEN 1 THEN 1 WHEN 2 THEN 2 WHEN 3 THEN 3 WHEN 4 THEN 4 
            WHEN 12 THEN 5 
            ELSE 6 
      END AS orderpriority 
FROM rates ORDER BY orderpriority,sno");

The above statement is returning false What can be the solution?

1
  • 1
    in case you are using multiple queries you need to use mysqli_free_result($result) between them Commented Dec 9, 2014 at 8:49

1 Answer 1

1

You could use multi_query of mysqli.

Or move initialization.

SELECT(@new_sno:=@new_sno+1) AS sno_new,Products,Price,case sno WHEN 1
THEN 1 WHEN 2 THEN 2 WHEN 3 THEN 3 WHEN 4 THEN 4 WHEN 12 THEN 5 ELSE 6 END AS
orderpriority 
FROM rates, (select @new_sno:=0) `tmp` -- there
ORDER BY orderpriority,sno;

Or simply execute it one by one.

Sign up to request clarification or add additional context in comments.

3 Comments

For background, mysqli_query() will only accept one query (delimited by a ;) to prevent SQL injection. They have implemented the multi_query option to circumvent this.
Hi.. I just searched the use of 'tmp' -- there, I cudn find much, as a matter of fact I dint understand the query,I want to know about it, can u enlighten me? any other resources other than mysql docs please?
@user2401175, it's derived table. Sorry, but better to start from mysql documentation: dev.mysql.com/doc/refman/5.6/en/from-clause-subqueries.html

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.