Re: Trouble passing mysql table name to php function and using it!



<kennthompson@xxxxxxxxx> wrote:
Jerry Stuckle <jstuck...@xxxxxxxxxxxxx> wrote:
kennthomp...@xxxxxxxxx wrote:
> Trouble passing mysql table name in php. If I use an existing table
> name already defined everything works fine as the following script
> illustrates.
> But it won't work if I pass a variable table name to the function.

> <?php
> function fms_get_info($tableName)
> {
> $result = mysql_query("select * from $tableName") ;
> for ($i = 0; $i < mysql_num_rows($result); $i++)
> {
> /* do something */
> }

> }
This should work fine. What do you get back as an error message? How
are you calling the function?

What happens if you do the following:

function fms_get_info($tableName)
{
$sql = "select * from $tableName";
}

No. That's exactly the way I was trying to do it. MySQL doesn't accept
a table name passed into a function this way. There must be some
hidden mysql code that I'm unaware of.

Jerry did ask for you to echo mysql_error...

But here you go:
function fms_get_info($tableName){
$qry = "SELECT * FROM `{$tableName}`";
$res = mysql_query($qry) or die($qry.' failed, mysql sais:'.mysql_error());
}

--
Rik Wasmus
.



Relevant Pages

  • Re: Script stops running during while()
    ... that processes records returned by a MySQL query. ... The HTML page continues trying to load the page but the php has stopped running and eventually I get a timeout. ... I have a new PC and had to re-install php, Apache and MySQL ... The script executed perfectly on my old PC ...
    (comp.lang.php)
  • [Full-Disclosure] [waraxe-2004-SA#031 - Multiple vulnerabilities in e107 version 0.615]
    ... mysql must be version 4.x with enabled UNION ... script - must be kept in secret, when possible, or it ... php error messages, ... Potential attacker can use xss to steal the cookies, ...
    (Full-Disclosure)
  • Re: newbie MySql script question
    ... how do I run this script? ... the best way to administer a mysql ... you have not written any php. ... or just start up mysql and type your SQL statements at the mysql prompt ...
    (comp.lang.php)
  • Script stops running during while()
    ... I have a php script which simply stops midway through a while loop ... that processes records returned by a MySQL query. ... - I have a new PC and had to re-install php, Apache and MySQL ...
    (comp.lang.php)
  • Re: [PHP] Code works alone but not with other code.
    ... place it alone in a php script it works just fine, ... the script seem like it's just skipping this chunk of code, ... The code does use nested MySQL queries, it worked before but I thought ...
    (php.general)