开发者

Returning column names from a mySQL table using PHP

Can开发者_JAVA技巧't figure this out for the life of me. Trying to return the column names from the clients securities table, then return the result as an array. Can anybody point out where I'm getting off track?

mysql_select_db("HandlerProject", $con);        //Selects database
  $selectcols = "SELECT * FROM ".$clientname."securitiestable";     //selects all     columns from clients security table
  $tempcols = mysql_query($selectcols) or die(mysql_error());
  $returnedcols = $mysql_fetch_array($tempcols);
  $tempsymbol = mysql_query("SHOW COLUMNS FROM".$clientname."securitiestable");
  $symbol = $mysql_fetch_array($tempsymbol);


Suggestions:

You've got $ signs prefixing the mysql_fetch_array() calls so you'd need to have assigned a value (function name you want to call) to $mysql_fetch_array (this is probably why you're seeing the error you mention in your comment).

Also you have a missing space after FROM in the second query

//                                          v
$tempsymbol = mysql_query("SHOW COLUMNS FROM ".$clientname."securitiestable");

Last thing to check - is $clientname set?

Having said that - take Bill Karwin's advice!


I would use mysql_fetch_assoc() for the SELECT query, and then call array_keys() on any row of the result.

$selectcols = "SELECT * FROM ".$clientname."securitiestable";
$tempcols = mysql_query($selectcols) or die(mysql_error());
$returnedcols = mysql_fetch_assoc($tempcols);
$colnames = array_keys($returnedcols);

Your fatal error is because of a separate issue: you have a $ symbol at the start of your function call. This is legal PHP syntax, because you can put the name of a function in a variable and call it indirectly:

function foo($arg)
{
  echo $arg . "!\n";
}

$bar = "foo";

$bar("hello world");

But in your case, it's probably not what you intended. If you want to call a function by its literal name, don't put a $ in front of it. If you have a string variable that contains the name of a function, then you can use the variable as I show above.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜