By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,967 Members | 1,684 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,967 IT Pros & Developers. It's quick & easy.

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

P: n/a
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.

Feb 27 '07 #1
Share this Question
Share on Google+
11 Replies


P: n/a
ke**********@gmail.com 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.
js*******@attglobal.net
==================
Feb 27 '07 #2

P: n/a
On Feb 27, 10:27 am, Jerry Stuckle <jstuck...@attglobal.netwrote:
kennthomp...@gmail.com 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...@attglobal.net
==================- 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.
Feb 27 '07 #3

P: n/a
Rik
<ke**********@gmail.comwrote:
Jerry Stuckle <jstuck...@attglobal.netwrote:
>kennthomp...@gmail.com 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
Feb 27 '07 #4

P: n/a
In article <op.toe7s3uqqnv3q9@misant>, Rik <lu************@hotmail.com>
wrote:
<ke**********@gmail.comwrote:
Jerry Stuckle <jstuck...@attglobal.netwrote:
kennthomp...@gmail.com 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());
}
Why does one need the back ticks and { ?? I would have expected one
could construct a query with:

$tab = "thistable";
$query = "select * from " . $tab;
$res = mysql_query ($query);

or thereabouts. What does the extra stuff do?

Thanks,
Feb 27 '07 #5

P: n/a
..oO(ke**********@gmail.com)
>On Feb 27, 10:27 am, Jerry Stuckle <jstuck...@attglobal.netwrote:
>>
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.
Then it would work, assuming that $tableName contains a proper table
name.
>MySQL doesn't accept
a table name passed into a function this way.
It doesn't matter whether the table name is hard-wired or stored in a
variable. The final query string as seen by MySQL will be the same.
>There must be some
hidden mysql code that I'm unaware of.
Nope. Just make use of the error reporting features that PHP/MySQL
offer.

Micha
Feb 27 '07 #6

P: n/a
On Feb 27, 2:46 pm, Michael Fesser <neti...@gmx.dewrote:
.oO(kennthomp...@gmail.com)
On Feb 27, 10:27 am, Jerry Stuckle <jstuck...@attglobal.netwrote:
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.

Then it would work, assuming that $tableName contains a proper table
name.
MySQL doesn't accept
a table name passed into a function this way.

It doesn't matter whether the table name is hard-wired or stored in a
variable. The final query string as seen by MySQL will be the same.
There must be some
hidden mysql code that I'm unaware of.

Nope. Just make use of the error reporting features that PHP/MySQL
offer.

Micha
I understand what you are saying, but I have written the code using
the same variable name that is first defined as the table name within
the function and everything works fine, but when I change it to a
variable name it does not work. I makes no sense to me. I'm just
telling you the facts. Here is an extract of the code: (By the way,
the working model only worked if I declared the outside table name
global. The following version does not include this statement. I've
tried dozens of variations. None seem to work passing the mysql table
name to the function.
function fms_get_table($tableName, $label, $name, $var)
{
if (is_array($var))
{
$var = $var[$name];
}
fms_open_table_row();
fms_open_table_col();
echo "$label:";
fms_close_table_col();
fms_close_table_row();
echo "<SELECT size=1 name=project>\n";
$result2 = fms_mysql_query("SELECT * FROM '{$tableName}'");
if ($result2)
{
$row2 = mysql_fetch_array($result2);
if (strlen($row2["projectName"]) 0)
{
echo "<OPTION VALUE=" . $row2["keyid"] .
">" .
$row2["projectName"] . "\n";
}
}
else
{
echo "<option&nbsp;\n";
}
$result2 = fms_mysql_query("select * from '{$tableName}' order
by
keyid asc");
for ($i = 0; $i < mysql_num_rows($result2); $i++)
{
$row2 = mysql_fetch_array($result2);
if (strlen($row2["projectName"]) 0)
{
echo "<OPTION VALUE=" . $row2["keyid"] .
">" .
$row2["projectName"] . "\n";
}
}
echo "</SELECT>\n";
fms_close_table_col();
fms_close_table_row();
}
/* Calling script: an edit form */

$query = "select * from $tableFields where keyid = '$keyid'";
$result = mysql_query($query);
if ($result)
{
$row = mysql_fetch_array($result);
}
fms_break(1);
fms_open_center();
fms_open_form("edit", "updateField.php", "post");
fms_open_table(0, "#eeeeee", "", 2, 2);
fms_hidden("keyid", $row);

fms_get_table($tableTarget, "Project", "project", $row);
fms_get_value("Label", "label", 60, $row);
fms_get_value("Field", "field", 60, $row);
fms_get_value("Type", "type", 60, $row);
fms_get_value("Parameters", "parameters", 60, $row);
fms_get_value("Attributes", "attributes", 60, $row);
fms_get_value("Extra", "extra", 60, $row);
fms_get_value("FMS", "fms", 60, $row);
fms_get_value("Cols", "cols", 10, $row);
fms_get_value("Rows", "rows", 10, $row);
fms_close_table();
fms_break(1);
fms_submit("UPDATE");
fms_submit("DELETE");
fms_submit("DUPLICATE");
fms_close_form();
fms_close_center();
/* mysql connection */
$host = "localhost";
$user = "";
$password = "";
$database = "";
$tableTarget = "php_generator_targets";
$tableFields = "php_generator_data_fields";
$link = mysql_pconnect( $host, $user, $password );
if(!$link)
{
echo "Did not connect.";
}
else
{
mysql_select_db($database);
if(mysql_errno())
{
echo mysql_errno() . ":" . mysql_error();
exit;
}
}
/* Main */
A shell gets the mysql connection, and manipulates the display. The
edit form tried to extract information from another table. It works
fine if I write a piece of code for each call, but I want the same
function to work for a number of database tables. But when I tried
passing the table name -- to my surprise it did not work. I've tried
numerous variations without success.
Feb 27 '07 #7

P: n/a
Rik
Tim Streater <ti*********@waitrose.comwrote:
>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());
}

Why does one need the back ticks and { ?? I would have expected one
could construct a query with:
Backticks, to make sure it even works when a table has a reserved
name('order' or something comes to mind). The accolades are just for my
benefit: I've taken to it to always use them in double quoted strings, it
doesn't cost anything extra, and you're always sure you haven't forgotten
when using array values or object properties.
--
Rik Wasmus
Feb 28 '07 #8

P: n/a
Rik
On Tue, 27 Feb 2007 23:47:17 +0100, <ke**********@gmail.comwrote:
$result2 = fms_mysql_query("SELECT * FROM '{$tableName}'");
backticks (`) are _not_ single quotes(')...

Use "SELECT * FROM `{$tableName}`"

--
Rik Wasmus
Feb 28 '07 #9

P: n/a
ke**********@gmail.com wrote:
On Feb 27, 10:27 am, Jerry Stuckle <jstuck...@attglobal.netwrote:
>kennthomp...@gmail.com 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...@attglobal.net
==================- 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.
js*******@attglobal.net
==================
Feb 28 '07 #10

P: n/a
In article <op.tofqiap6qnv3q9@misant>, Rik <lu************@hotmail.com>
wrote:
Tim Streater <ti*********@waitrose.comwrote:
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());
}
Why does one need the back ticks and { ?? I would have expected one
could construct a query with:

Backticks, to make sure it even works when a table has a reserved
name('order' or something comes to mind). The accolades are just for my
benefit: I've taken to it to always use them in double quoted strings, it
doesn't cost anything extra, and you're always sure you haven't forgotten
when using array values or object properties.
Ah, thanks, that's clear. In fact I don't usually have a variable
tablename when constructing a query so I haven't been hit by that.

But a question to the OP might be - what debugging have you done?
Whenever I get an oddity of this nature I usually spend some time
carefully debugging before posting about it.

-- tim
Feb 28 '07 #11

P: n/a
Rik
Jerry Stuckle <js*******@attglobal.netwrote:
It's not PHP nor MySQL that's your problem.
Tssssk :P.
--
Rik Wasmus
Mar 1 '07 #12

This discussion thread is closed

Replies have been disabled for this discussion.