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



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.


.



Relevant Pages

  • Re: Does apache stop a script mid execution ?
    ... If the user calls a time consuming script and then stops or refreshes ... How does it relate to e.g. a script performing a large mysql query? ... 2.b) how the connection is set up. ... the server stops the script execution. ...
    (comp.lang.php)
  • Re: Does apache stop a script mid execution ?
    ... If the user calls a time consuming script and then stops or refreshes ... the way the client is implemented in the php mysql extension and 2.b) how the connection is set up. ... the server stops the script execution. ...
    (comp.lang.php)
  • Re: Cyrus IMAP with pam_mysql?
    ... FreeBSD, but if you check the startup script, all the parameters are ... Default log directories are in the default data directory, ... Makefile for mysql) They are changed in the above script prior to ... imaps auth sufficient pam_unix.so ...
    (freebsd-questions)
  • Re: MySQL to SQL
    ... basically I want to take a MySql script and load it into a SQL database. ... BIOS_RELEASE_DATE datetime default NULL, ... CUSTOM_TEXT_FIELD1 mediumtext, ...
    (microsoft.public.scripting.wsh)
  • Re: php script to create mySQL database programmatically
    ... hosting configuration may not allow create database from script, ... Jerry Stuckle ... JDS Computer Training Corp. ... Can't connect to local MySQL server through socket ...
    (comp.lang.php)