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



kennthompson@xxxxxxxxx wrote:
On Feb 27, 10:27 am, 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.
<?php
function fms_get_info()
{
$result = mysql_query("select * from $tableInfo") ;
for ($i = 0; $i < mysql_num_rows($result); $i++)
{
/* do something */
}
}
/* Main */
fms_get_info();
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 */
}
}
/* Main */
fms_get_info($tableInfo);
I need to use the same function to gather information from multiple
tables at will without creating a different function for each
possible
mysql database table by name. I thought this would be easy, but I
have failed at several tries.
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";
echo $sql . "<br>\n";
$result = mysql_query($sql) ;
for ($i = 0; $i < mysql_num_rows($result); $i++)
{
/* do something */
}

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstuck...@xxxxxxxxxxxxx
==================- Hide quoted text -

- Show quoted text -



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.



It works. Did you do as I asked? Echo the sql first, then the error returned by mysql.

It's not PHP nor MySQL that's your problem.


--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@xxxxxxxxxxxxx
==================
.



Relevant Pages

  • Re: MySQL connect failure
    ... I didn't add the database to the mysqli parameter list. ... The "mysqli" is the very first MySQL command I ... Jerry Stuckle ... JDS Computer Training Corp. ...
    (comp.lang.php)
  • Re: MySQL connect failure
    ... I didn't add the database to the mysqli parameter list. ... The "mysqli" is the very first MySQL command I ... Jerry Stuckle ... JDS Computer Training Corp. ...
    (comp.lang.php)
  • Re: MySQL connect failure
    ... I didn't add the database to the mysqli parameter list. ... The "mysqli" is the very first MySQL ... Jerry Stuckle ... JDS Computer Training Corp. ...
    (comp.lang.php)
  • Re: I am totally stumped..with this on..LOAD_FILE Mysql+PHP= FSCK!!
    ... What I am trying to do is to upload files and stuff them in a mysql database. ... I copied the temporary file to somewhere else, and then handed it to MySQL..THAT WORKED.. ... Is there a way to force a close on the file..maybe that's the problem Mysql is opening a file that is not flushed to disk maybe? ... I gew the feeling its maintaining its own picture of file objects, and doesn't actually flush to the disk unless you do a copy or close php.. ...
    (comp.lang.php)
  • Re: MySQL connect failure
    ... I didn't add the database to the mysqli parameter list. ... The "mysqli" is the very first MySQL ... Jerry Stuckle ... JDS Computer Training Corp. ...
    (comp.lang.php)