我有一个可以通过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;
但是当我尝试从php文件运行它时,我没有返回任何行。
我从Google上了解到,从php运行多个查询时出现问题,我看到一个提示建议存储过程。我也尝试了这个,但它引发了一个错误:
$this->_db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
$stmt = $this->_db->prepare($part1);
$stmt->execute();
但是在大多数情况下我并不理解我找到的几个答案(我的PHP或SQL都没有提高)。
以前有人这样做过吗?
由于 艾玛
答案 0 :(得分:0)
这段代码对我来说很棒!
<?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());
?>