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 Tim Streater on 02/27/07 20:42

In article <op.toe7s3uqqnv3q9@misant>, Rik <luiheidsgoeroe@hotmail.com>
wrote:

> <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 = 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,

 

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

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