472,368 Members | 2,611 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,368 software developers and data experts.

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

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
11 3580
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
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
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
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
..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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Penn Markham | last post by:
Hello all, I am writing a script where I need to use the system() function to call htpasswd. I can do this just fine on the command line...works great (see attached file, test.php). When my...
0
by: Mike Chirico | last post by:
Interesting Things to Know about MySQL Mike Chirico (mchirico@users.sourceforge.net) Copyright (GPU Free Documentation License) 2004 Last Updated: Mon Jun 7 10:37:28 EDT 2004 The latest...
6
by: dharmadam | last post by:
Is it possible to pass a column name or the order of the column name in the DB2 table table function. For example, I want to update the address of a person by passing one of the address column name...
6
by: Ted | last post by:
Here is one such function: CREATE FUNCTION my_max_market_date () RETURNS datetime BEGIN DECLARE @mmmd AS datetime; SELECT max(h_market_date) INTO @mmmd FROM holdings_tmp; RETURN @mmmd; END ...
4
by: assgar | last post by:
Hi I am stuck on a problem. I use 3 scripts(form, function and process). Development on win2003 server. Final server will be linux Apache,Mysql and PHP is being used. The form displays...
5
by: strawberry | last post by:
In the function below, I'd like to extend the scope of the $table variable such that, once assigned it would become available to other parts of the function. I thought 'global $table;' would solve...
3
by: Joshepmichel | last post by:
Please to help me to following problem I want to do this 1. create Table Name MEMBER on the Database Name "mytestdb", 2. Add the Values to the Table through the Key board Inputs during running...
3
by: janetopps | last post by:
I have a news website, with asp pages, which was on Access, and i upgraded to MySQL, i used Bullzip to transfer the data. It had about 1000 pages, which im now able to pull up on the public side. Im...
1
ssnaik84
by: ssnaik84 | last post by:
Hi Guys, Last year I got a chance to work with R&D team, which was working on DB scripts conversion.. Though there is migration tool available, it converts only tables and constraints.. Rest of...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
2
by: Ricardo de Mila | last post by:
Dear people, good afternoon... I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control. Than I need to discover what...
1
by: Johno34 | last post by:
I have this click event on my form. It speaks to a Datasheet Subform Private Sub Command260_Click() Dim r As DAO.Recordset Set r = Form_frmABCD.Form.RecordsetClone r.MoveFirst Do If...
1
by: ezappsrUS | last post by:
Hi, I wonder if someone knows where I am going wrong below. I have a continuous form and two labels where only one would be visible depending on the checkbox being checked or not. Below is the...
0
by: jack2019x | last post by:
hello, Is there code or static lib for hook swapchain present? I wanna hook dxgi swapchain present for dx11 and dx9.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.