Connecting Tech Pros Worldwide Help | Site Map

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

  #1  
Old February 27th, 2007, 03:55 PM
kennthompson@gmail.com
Guest
 
Posts: 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.

  #2  
Old February 27th, 2007, 04:35 PM
Jerry Stuckle
Guest
 
Posts: n/a

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


kennthompson@gmail.com wrote:
Quote:
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.
jstucklex@attglobal.net
==================
  #3  
Old February 27th, 2007, 06:25 PM
kennthompson@gmail.com
Guest
 
Posts: n/a

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


On Feb 27, 10:27 am, Jerry Stuckle <jstuck...@attglobal.netwrote:
Quote:
kennthomp...@gmail.com wrote:
Quote:
Trouble passing mysql table name in php. If I use an existing table
name already defined everything works fine as the following script
illustrates.
>
Quote:
<?php
function fms_get_info()
{
$result = mysql_query("select * from $tableInfo") ;
for ($i = 0; $i < mysql_num_rows($result); $i++)
{
/* do something */
}
>
Quote:
}
>
Quote:
/* Main */
fms_get_info();
>
Quote:
But it won't work if I pass a variable table name to the function.
>
Quote:
<?php
function fms_get_info($tableName)
{
$result = mysql_query("select * from $tableName") ;
for ($i = 0; $i < mysql_num_rows($result); $i++)
{
/* do something */
}
>
Quote:
}
>
Quote:
/* Main */
fms_get_info($tableInfo);
>
Quote:
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.


  #4  
Old February 27th, 2007, 06:55 PM
Rik
Guest
 
Posts: n/a

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


<kennthompson@gmail.comwrote:
Quote:
Jerry Stuckle <jstuck...@attglobal.netwrote:
Quote:
>kennthomp...@gmail.com wrote:
Quote:
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.
>>
Quote:
<?php
function fms_get_info($tableName)
{
$result = mysql_query("select * from $tableName") ;
for ($i = 0; $i < mysql_num_rows($result); $i++)
{
/* do something */
}
>>
Quote:
}
>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
  #5  
Old February 27th, 2007, 08:55 PM
Tim Streater
Guest
 
Posts: n/a

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


In article <op.toe7s3uqqnv3q9@misant>, Rik <luiheidsgoeroe@hotmail.com>
wrote:
Quote:
<kennthompson@gmail.comwrote:
Quote:
Jerry Stuckle <jstuck...@attglobal.netwrote:
Quote:
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,
  #6  
Old February 27th, 2007, 08:55 PM
Michael Fesser
Guest
 
Posts: n/a

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


..oO(kennthompson@gmail.com)
Quote:
>On Feb 27, 10:27 am, Jerry Stuckle <jstuck...@attglobal.netwrote:
Quote:
>>
>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.
Quote:
>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.
Quote:
>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
  #7  
Old February 27th, 2007, 10:55 PM
kennthompson@gmail.com
Guest
 
Posts: n/a

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


On Feb 27, 2:46 pm, Michael Fesser <neti...@gmx.dewrote:
Quote:
.oO(kennthomp...@gmail.com)
>
Quote:
On Feb 27, 10:27 am, Jerry Stuckle <jstuck...@attglobal.netwrote:
>
Quote:
Quote:
What happens if you do the following:
>
Quote:
Quote:
function fms_get_info($tableName)
{
$sql = "select * from $tableName";
[...]
>
Quote:
No. That's exactly the way I was trying to do it.
>
Then it would work, assuming that $tableName contains a proper table
name.
>
Quote:
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.
>
Quote:
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.


  #8  
Old February 28th, 2007, 01:35 AM
Rik
Guest
 
Posts: n/a

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


Tim Streater <timstreater@waitrose.comwrote:
Quote:
Quote:
>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
  #9  
Old February 28th, 2007, 01:35 AM
Rik
Guest
 
Posts: n/a

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


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

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

--
Rik Wasmus
  #10  
Old February 28th, 2007, 02:05 AM
Jerry Stuckle
Guest
 
Posts: n/a

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


kennthompson@gmail.com wrote:
Quote:
On Feb 27, 10:27 am, Jerry Stuckle <jstuck...@attglobal.netwrote:
Quote:
>kennthomp...@gmail.com wrote:
Quote:
>>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.
jstucklex@attglobal.net
==================
  #11  
Old February 28th, 2007, 10:05 AM
Tim Streater
Guest
 
Posts: n/a

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


In article <op.tofqiap6qnv3q9@misant>, Rik <luiheidsgoeroe@hotmail.com>
wrote:
Quote:
Tim Streater <timstreater@waitrose.comwrote:
Quote:
Quote:
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
  #12  
Old March 1st, 2007, 03:45 AM
Rik
Guest
 
Posts: n/a

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


Jerry Stuckle <jstucklex@attglobal.netwrote:
Quote:
It's not PHP nor MySQL that's your problem.
Tssssk :P.
--
Rik Wasmus
Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
OOP - Problems with adapting some code chromis answers 2 July 31st, 2008 03:24 PM
php extensions and windows... specifically extension_dir Chris Paul answers 3 December 5th, 2005 12:45 AM
Trouble with system() function Penn Markham answers 9 July 17th, 2005 05:43 AM
need feed back about the comments I write lawrence answers 2 July 17th, 2005 05:22 AM