简体   繁体   中英

Unable to execute query in PHP

I have a SQL query which works perfectly if I am running it manually through PhpMyAdmin, however, it is unable to execute when executing from PHP.

Query:

LOCK TABLE table_name WRITE;

SELECT @myRight := rgt FROM table_name
WHERE name = 'feildname';

UPDATE table_name SET rgt = rgt + 2 WHERE rgt > @myRight;
UPDATE table_name SET lft = lft + 2 WHERE lft > @myRight;

INSERT INTO table_name(name, lft, rgt) VALUES('new_feild_value', @myRight + 1, @myRight + 2);

UNLOCK TABLES;

I want to run the query through my PHP page which has new_feild_value variable taken from user input, which can be seen in the code below:

<?php
$newname = $_POST['newname'];
$sqlquery = 'LOCK TABLE table_name WRITE;

    SELECT @myRight := rgt FROM table_name
    WHERE name = "feildname";

    UPDATE table_name SET rgt = rgt + 2 WHERE rgt > @myRight;
    UPDATE table_name SET lft = lft + 2 WHERE lft > @myRight;

    INSERT INTO table_name(name, lft, rgt) VALUES("' .$newname . '", @myRight + 1, @myRight + 2);

    UNLOCK TABLES;';
if(!mysqli_query($link,$sqlquery)){ //$link is variable to make sql connection
                echo 'error inserting the comment';
            exit();
            }
 echo 'successfully inserted the values';
?>

The PHP snippet above won't work, but for the same snippet, other simple queries are working. What is the issue and how can I fix it?

You must use mysqli_multi_query to run multiple queries in one statement with mysqli. http://php.net/manual/en/mysqli.multi-query.php

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM