简体   繁体   English

PHP | MySQL | 多个查询以使用information_schema列

[英]PHP | MySQL | Multiple query to use information_schema columns

I have a query that works via phpMyAdmin: 我有一个通过phpMyAdmin工作的查询:

SET @sql = CONCAT('SELECT ', (SELECT GROUP_CONCAT(COLUMN_NAME) FROM information_schema.columns where table_name='staff' and table_schema='tag'), ' FROM tag.staff WHERE staff_id=1;'); 
PREPARE stmt1 FROM @sql; 
EXECUTE stmt1;

But when I try to run it from a php file, I get no rows returned. 但是,当我尝试从php文件运行它时,没有返回任何行。

I understand from googling that there's a problem with running multiple queries from php and one tip I saw suggested a stored procedure. 从谷歌搜索中我了解到,从php运行多个查询存在问题,我看到一个提示建议存储过程。 I also tried this but it threw an error: 我也试过了,但是它抛出了一个错误:

$this->_db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
$stmt = $this->_db->prepare($part1);
$stmt->execute();

But for the most part I didn't understand the few answers I found (neither my PHP or SQL are advanced). 但是在大多数情况下,我不明白我找到的几个答案(我的PHP或SQL都不是高级的)。

Has anyone done this before? 有人做过吗?

Thanks Emma 谢谢艾玛

This code worked for me bevor! 这段代码对我有用!

<?php
$mysqli = new mysqli("example.com", "user", "password", "database");
if ($mysqli->connect_errno) {
    echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}

if (!$mysqli->query("DROP TABLE IF EXISTS test") || !$mysqli->query("CREATE TABLE test(id INT)")) {
    echo "Table creation failed: (" . $mysqli->errno . ") " . $mysqli->error;
}

$sql = "SELECT COUNT(*) AS _num FROM test; ";
$sql.= "INSERT INTO test(id) VALUES (1); ";
$sql.= "SELECT COUNT(*) AS _num FROM test; ";

if (!$mysqli->multi_query($sql)) {
    echo "Multi query failed: (" . $mysqli->errno . ") " . $mysqli->error;
}

do {
    if ($res = $mysqli->store_result()) {
        var_dump($res->fetch_all(MYSQLI_ASSOC));
        $res->free();
    }
} while ($mysqli->more_results() && $mysqli->next_result());
?>

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

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