473,890 Members | 1,716 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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("se lect * 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($t ableName)
{
$result = mysql_query("se lect * from $tableName") ;
for ($i = 0; $i < mysql_num_rows( $result); $i++)
{
/* do something */
}

}
/* Main */
fms_get_info($t ableInfo);

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 3835
ke**********@gm ail.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("se lect * 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($t ableName)
{
$result = mysql_query("se lect * from $tableName") ;
for ($i = 0; $i < mysql_num_rows( $result); $i++)
{
/* do something */
}

}
/* Main */
fms_get_info($t ableInfo);

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($t ableName)
{
$sql = "select * from $tableName";
echo $sql . "<br>\n";
$result = mysql_query($sq l) ;
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*******@attgl obal.net
=============== ===
Feb 27 '07 #2
On Feb 27, 10:27 am, Jerry Stuckle <jstuck...@attg lobal.netwrote:
kennthomp...@gm ail.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("se lect * 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($t ableName)
{
$result = mysql_query("se lect * from $tableName") ;
for ($i = 0; $i < mysql_num_rows( $result); $i++)
{
/* do something */
}
}
/* Main */
fms_get_info($t ableInfo);
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($t ableName)
{
$sql = "select * from $tableName";
echo $sql . "<br>\n";
$result = mysql_query($sq l) ;
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...@attgl obal.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**********@g mail.comwrote:
Jerry Stuckle <jstuck...@attg lobal.netwrote:
>kennthomp...@g mail.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($t ableName)
{
$result = mysql_query("se lect * 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($t ableName)
{
$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($t ableName){
$qry = "SELECT * FROM `{$tableName}`" ;
$res = mysql_query($qr y) or die($qry.' failed, mysql
sais:'.mysql_er ror());
}

--
Rik Wasmus
Feb 27 '07 #4
In article <op.toe7s3uqqnv 3q9@misant>, Rik <lu************ @hotmail.com>
wrote:
<ke**********@g mail.comwrote:
Jerry Stuckle <jstuck...@attg lobal.netwrote:
kennthomp...@gm ail.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...@attg lobal.netwrote:
>>
What happens if you do the following:

function fms_get_info($t ableName)
{
$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.de wrote:
.oO(kennthomp.. .@gmail.com)
On Feb 27, 10:27 am, Jerry Stuckle <jstuck...@attg lobal.netwrote:
What happens if you do the following:
function fms_get_info($t ableName)
{
$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_arr ay($result2);
if (strlen($row2["projectNam e"]) 0)
{
echo "<OPTION VALUE=" . $row2["keyid"] .
">" .
$row2["projectNam e"] . "\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_arr ay($result2);
if (strlen($row2["projectNam e"]) 0)
{
echo "<OPTION VALUE=" . $row2["keyid"] .
">" .
$row2["projectNam e"] . "\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($qu ery);
if ($result)
{
$row = mysql_fetch_arr ay($result);
}
fms_break(1);
fms_open_center ();
fms_open_form(" edit", "updateField.ph p", "post");
fms_open_table( 0, "#eeeeee", "", 2, 2);
fms_hidden("key id", $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("UPD ATE");
fms_submit("DEL ETE");
fms_submit("DUP LICATE");
fms_close_form( );
fms_close_cente r();
/* 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*********@wa itrose.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**********@g mail.comwrote:
$result2 = fms_mysql_query ("SELECT * FROM '{$tableName}'" );
backticks (`) are _not_ single quotes(')...

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

--
Rik Wasmus
Feb 28 '07 #9
ke**********@gm ail.com wrote:
On Feb 27, 10:27 am, Jerry Stuckle <jstuck...@attg lobal.netwrote:
>kennthomp...@g mail.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("se lect * 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($t ableName)
{
$result = mysql_query("se lect * 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($t ableName)
{
$sql = "select * from $tableName";
echo $sql . "<br>\n";
$result = mysql_query($sq l) ;
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...@attg lobal.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*******@attgl obal.net
=============== ===
Feb 28 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
4970
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 webserver runs that part of the script (see attached file, snippet.php), though, it doesn't go through. I don't get an error message or anything...it just returns a "1" (whereas it should return a "0") as far as I can tell. I have read the PHP...
0
3959
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 version of this document can be found at: http://prdownloads.sourceforge.net/souptonuts/README_mysql.txt?download
6
6302
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 like ZIP CODE or ADDRESS LINE. I will call the function with three parameter--UpdateAddress(5,zip_code,person_id) where 5 indicates ZIP_CODE is the fifth column in the table. If 4 is passed, it indicates the address line is to be updated. ...
6
1476
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 One change I had to make, relative to what I had working in MySQL, was
4
2720
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 multiple dynamic rows with chechboxs, input box for units of service, description of the service and each row has its own dropdown list of unit fees that apply.
5
2285
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 this but it's clear that I'm misunderstanding $variable persistence. I posted a similar enquiry over at alt.php.mysql, but I guess this is a more appropriate forum because the problems I'm having relate to PHP. Any help appreciated. ...
3
7708
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 the My Java Application.; Therefore I used this following codes, It Consists the seperate parts for the Raede and Member Class for the purposely I craetes. 3. I have problem to Parsing the values to SQL statement, that Consists on the Run Time...
3
3554
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 sorting out a few glitches though. Since i upgraded from ms access database to MySQL, i have added about 4 articles to test the new setup. I note some fields aren't being added in the new mySql database for the new 4 records. When i ran the MySQK...
1
9604
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 things (stored procedures, functions).. we have to manually edit. That time, we face some interesting challenges.. I failed to document all of them, but whatever I can share with u.. I will try.. :) ...
0
11215
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10908
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9618
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
8008
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7160
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5835
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6035
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4259
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3267
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.