2013-07-31 62 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

谢谢你的工作, 我想你忘了 **返回数组(); ** –