简体   繁体   English

无法在PHP中执行查询

[英]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. 我有一个SQL查询,如果我通过PhpMyAdmin手动运行它可以很好地工作,但是,从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页面运行查询,该页面具有取自用户输入的new_feild_value变量,可以在下面的代码中看到:

<?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. 上面的PHP代码段不起作用,但对于相同的代码段,其他简单查询正在运行。 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. 您必须使用mysqli_multi_query在一个语句中使用mysqli运行多个查询。 http://php.net/manual/en/mysqli.multi-query.php http://php.net/manual/en/mysqli.multi-query.php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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