You are here: Re: Trouble passing mysql table name to php function and using it! « PHP Programming Language « IT news, forums, messages
Re: Trouble passing mysql table name to php function and using it!

Posted by kennthompson on 02/27/07 22:47

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...@attglobal.net> wrote:
>
> >> 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.

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация