php - Do mysql_list_fields for a Join Query -


i looking populate array of columns being viewed in query lets say

    $sql='select a.tutorial_id, a.tutorial_author, b.tutorial_count     tutorials_tbl a, tcount_tbl b     a.tutorial_author = b.tutorial_author';       function getcoloumns($sql) {          $result = mysql_query("show columns (". $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;     } 

i cant seem right. out there can me out. in advance.

show columns not allow subselect statement according mysql documentation.

you instead go combination of original query, mysql_num_fields() , mysql_field_name():

function getcoloumns($sql) {   $result = mysql_query( $sql );   if (!$result) {     echo 'could not run query: ' . mysql_error();     // return function here. don't know value need.     return array()   }    $fieldnames = array();   $fieldcount = mysql_num_fields($result);   for( $i=0; $i<$fieldcount; $i++ ) {     $fieldnames[] = mysql_field_name( $result , $i );   }    return $fieldnames; } 

besides, take @ why shouldn't use mysql_* functions in php? and/or make sure query not malicious!


Comments

Popular posts from this blog

c++ - Creating new partition disk winapi -

Android Prevent Bluetooth Pairing Dialog -

VBA function to include CDATA -