简体   繁体   English

MYSQL变量在PHP查询

[英]MYSQL variable In php query

I am trying to query multiple mysql statement in one query but failed. 我试图在一个查询中查询多个mysql语句,但失败了。

$testq ="";
        $testq .= mysql_query("SET @@group_concat_max_len = 5000");
        $testq .= mysql_query("SET @sql = NULL");
        $testq .= mysql_query("SELECT   GROUP_CONCAT(DISTINCT
                    CONCAT(
                      'MAX(IF(property_name = ''',
                      property_name,
                      ''', value, NULL)) AS ',
                      property_name
                    )
                  ) INTO @sql FROM properties");
        $testq .= mysql_query("SET @sql = CONCAT('SELECT item_id, ', @sql, ' FROM properties GROUP BY item_id')");
        $testq .= mysql_query("PREPARE stmt FROM @sql");
        $testq .= mysql_query("EXECUTE stmt"); 
        $da = mysql_query($testq, $dbconnect);          
        while($rt = mysql_fetch_assoc($da){
            //print
             }

Error is: Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in C:\\Users.....on line 24 How can I do this query and print the result in table? 错误是:警告:mysql_fetch_assoc()期望参数1为资源,在第24行的C:\\ Users ......中给定布尔值。如何执行此查询并在表中打印结果?

The MySQL extension in PHP does not support multi queries in PHP for security reasons with mysql_query . 出于安全原因,PHP中的MySQL扩展不支持mysql_query多查询。 You should not use MySQL anymore furthermore, use MySQL i . 您不应该再使用MySQL,请使用MySQL i

You should use UNION or something like this to concat the queries. 您应该使用UNION或类似的方式来连接查询。 If you just want to make a multi query, you should use mysqli_multi_query within the MySQL i library. 如果只想进行多重查询,则应在MySQL i库中使用mysqli_multi_query

Then the syntax have to be another. 然后语法必须是另一种。 The queries have to be submitted as one string in one function like this: 必须在一个函数中将查询作为一个字符串提交,如下所示:

$mysqli->multi_query("SELECT * FROM table; TRUNCATE table; DROP table;");

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

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