I have next code:
SET @rownum=0;
UPDATE product_images AS t, (SELECT @rownum:=@rownum+1 rownum, id, rel
FROM product_images WHERE product_id='227') AS r
SET t.rel = r.rownum
WHERE t.id = r.id
This is working excellent in phpmyadmin
BUT … next code (witch is actually the same) but placed in php code
mysql_query ("
SET @rownum=0;
UPDATE product_images AS t,
(SELECT @rownum:=@rownum+1 rownum, product_images.*
FROM product_images WHERE product_id='$pid') AS r
SET t.rel = r.rownum WHERE t.id = r.id ") or die(mysql_error());
GIVES ME ERROR : "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 'UPDATE product_images AS t, (SELECT @rownum:=@rownum+1 rownum, product_images.* ' at line 1"
PLEASE HELP. THANK YOU.
These are 2 queries you try to execute at once. That does not work with the
mysql_queryPHP method.You actually don’t need the first statement. Try
to init the
@rownumvariable on the fly.Simplified SQLFiddle example