2013-07-31 61 views
0

我期待來填充在查詢被觀看的所有列的一個陣列可以mysql_list_fields說做連接查詢

$sql='SELECT a.tutorial_id, a.tutorial_author, b.tutorial_count 
    FROM tutorials_tbl a, tcount_tbl b 
    WHERE a.tutorial_author = b.tutorial_author'; 


    function getColoumns($sql) { 
     $result = mysql_query("SHOW COLUMNS FROM (". $sql.")); 
    if (!$result) { 
    echo 'Could not run query: ' . mysql_error(); 
    } 
    $fieldnames=array(); 
    if (mysql_num_rows($result) > 0) { 
    while ($row = mysql_fetch_assoc($result)) { 
     $fieldnames[] = $row['Field']; 
    } 
    } 

    return $fieldnames; 
    } 

我似乎無法得到它的權利。任何人都可以幫助我。 在此先感謝。

+0

除了明顯缺少''',任何錯誤信息?你能更詳細地描述你的問題嗎? – Sirko

+0

OOPs對不起,應該是。$ sql)我以前試過,這只是我寫的 這是一個mysql_fetch_array()期望參數1是資源,布爾值 –

回答

1

SHOW COLUMNS不允許根據MySQL documentation進行子選擇聲明。

你可以代替去原始查詢的組合,mysql_num_fields()mysql_field_name()

function getColoumns($sql) { 
    $result = mysql_query($sql); 
    if (!$result) { 
    echo 'Could not run query: ' . mysql_error(); 
    // return from function here. don't know what value you need. 
    return array() 
    } 

    $fieldnames = array(); 
    $fieldCount = mysql_num_fields($result); 
    for($i=0; $i<$fieldCount; $i++) { 
    $fieldnames[] = mysql_field_name($result , $i); 
    } 

    return $fieldnames; 
} 

此外,看一看Why shouldn't I use mysql_* functions in PHP?和/或確保查詢是不是惡意的!

+0

無法運行查詢:您的SQL語法有錯誤;請查看與您的MySQL服務器版本對應的手冊,以獲取正確的語法使用附近的'SELECT a.tutorial_id,a.tutorial_author,b.tutorial_count FROM tuto'at line 1 –

+0

@GeoffreyMureithi對不起,沒有仔細閱讀你的問題,請仔細閱讀更新後的答案 – Sirko

+0

謝謝你的工作, 我想你忘了 **返回數組(); ** –