View Full Version : Change a table name with PHP

08-03-2007, 07:09 PM
I'm kind of new to php mysql but was wondering if it's possible to change a tables' name.

Let's say I have a News sections on my site that has posts each day there is new news. It echoes the contents out from a table that a user edits so that it displays date, title, content, etc. The name of the entire section and the table name is News. But let's say the user wants to change it from a News section to a Blog section, or whatever. Is there a way, on an edit page, to echo out the name of the table (news) in a text box and has the ability for the user to change it to something else, Blog for example, so that the table is now called blog, the section is now called blog, but the posts are all still there.

I know that to echo out the table name is pretty easy if I hardcode it into the page...something like:

$query="SELECT * FROM $tablename";

But the user wouldn't have the ability to change the $tablename variable I don't think, right?

Anyone know of a way to do that?
Thanks, Matt

08-03-2007, 07:50 PM
yes you can do it that way -- it's unconventional -- but it's doable

08-03-2007, 08:50 PM
but the user can't change $tablename with this code, can they?

what would be conventional?

Thanks for the reply, Matt

08-04-2007, 02:39 AM
just pass a GET or POST variable from a drop-down list or a hidden field etc..

$table = 'news';
$table = 'blog';
$sql="SELECT * FROM $table";