2014-10-02 96 views
1

我有通過phpMyAdmin的工作方式查詢:PHP | MySQL |多個查詢使用INFORMATION_SCHEMA列

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文件運行它,我沒有得到任何行返回。

我知道從谷歌搜索,從php運行多個查詢有一個問題,我看到一個提示存儲過程。我也試過,但它扔了一個錯誤:

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

但在大多數情況下我不明白的幾個答案,我發現(既不是我的PHP或SQL是高級)。

有沒有人這樣做呢?

感謝 艾瑪

回答

0

此代碼爲我工作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()); 
?> 
+0

謝謝 - 這看起來類似於我試圖遵循的示例。但困難在於使用INFORMATION_SCHEMA。我不只是試圖將一個變量替換爲WHERE子句:我試圖選擇的列是變量。 – emma 2014-10-02 11:34:50