Posted by Rik on 02/27/07 18:47
<kennthompson@gmail.com> wrote:
> Jerry Stuckle <jstuck...@attglobal.net> wrote:
>> 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 =3D mysql_query("select * from $tableName") ;
>> > for ($i =3D 0; $i < mysql_num_rows($result); $i++)
>> > {
>> > /* do something */
>> > }
>>
>> > }
>> This should work fine. What do you get back as an error message? Ho=
w
>> are you calling the function?
>>
>> What happens if you do the following:
>>
>> function fms_get_info($tableName)
>> {
>> $sql =3D "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 =3D "SELECT * FROM `{$tableName}`";
$res =3D mysql_query($qry) or die($qry.' failed, mysql =
sais:'.mysql_error());
}
-- =
Rik Wasmus
[Back to original message]
|