Re: Passing mysql table names to php functions?



kennthompson@xxxxxxxxx wrote:

function fms_get_table($tableName, $label, $name, $var)
{
if (is_array($var))
{
$var = $var[$name];
}
fms_open_table_row();
fms_open_table_col();
I assume thise functions do not have anything to do with the mysql, but echos table-tags like <tr>, <td> and so on.

echo "$label:";
fms_close_table_col();
fms_close_table_row();
echo "<SELECT size=1 name=project>\n";
$result2 = fms_mysql_query("SELECT * FROM '{$tableName}'");
You have the wrong quote type for the table name, and for debugging I would use a string to store the query (note: ' != `):

$query = "SELECT * FROM `{$tableName}`";
echo $query."<br>\n";
$result2 = fms_mysql_query($query);
echo mysql_error();

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");

Wrong quite type around the table name again, use ``, not ''

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);

As it looks here, the $tableTarget is an empty variable, which would lead to a "SELECT * FROM ", which generates in an error message from the mysql server, you don't see it as you don't check your values and report about failure, you shouldn't assume that a function will always work.

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 );

You may want to use mysql_connect() instead, pconnect works ok if there is a low activity and the same database is used all the time.

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.

Check that the variable with the table name isn't an array.



--

//Aho
.



Relevant Pages

  • Re: mysql scaling questions
    ... serious mysql scaling problems at concurrency> 8 threads ... # cat sysbench.common ... echo "Unknown dbtype"; ... echo $cmd ...
    (freebsd-performance)
  • Re: [PHP] Problem Understanding Code in 2nd edition Welling/Thomson PHP?MySQL Web Development Book
    ... ST> the 2nd edition of Welling/Thomson's PHP and MySQL Web Development as a ... ST> So I went into MySQL and created the database auth and the table auth, ... ST> echo 'Cannot connect to database.'; ...
    (php.general)
  • form to insert with foreign key
    ... I want a form which will query px_items along the lines of: ... my database and first working script: ... mysql> show databases; ... echo $row; ...
    (php.general)
  • Re: Manipulating date with "00" in it coming from mySQL
    ... It's not something I usually need to do so I am at a loss on how to accomplish it. ... I am trying to manipulate those dates to drop the zeros and only print out the year. ... Additionally, your tests are incorrect - $arrayDatacannot be both '00' and '', so your echo statement will never execute. ... Just parse the date returned by MySQL directly. ...
    (comp.lang.php)
  • Re: PHP Blank Page.
    ... echo mysql_error, ... Rik Wasmus ... I can clearly see that's not the query you are performing. ... mysql> select * from questions; ...
    (comp.lang.php)