0

Can'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);
2
  • The last two lines should work fine. What's the problem? Commented Jan 17, 2010 at 1:27
  • Throws a "Fatal error: Function name must be a string" for the $returnedcols and $symbol lines Commented Jan 17, 2010 at 1:40

2 Answers 2

5

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!

Sign up to request clarification or add additional context in comments.

Comments

3

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.

2 Comments

I know this post is old, but I'm just curious; Your method above - Will that not select EVERY row in the DB? When the SHOW COLUMNS FROM would just grab the column names? Excuse my dumbness.
If you use mysql_unbuffered_query() you can fetch just the first row.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.