Scruffy Posted December 9, 2009 Share Posted December 9, 2009 Hi, I was wondering if anyone knows if there is away to make the selection of table from MYSQL to be a php variable. Example is below: $id = $_GET['id']; $query_tester= "SELECT * FROM `$id` WHERE db_title = '$id'"; Thanks for your help! Quote Link to comment https://forums.phpfreaks.com/topic/184485-make-mysql-table-a-php-variable/ Share on other sites More sharing options...
MadTechie Posted December 9, 2009 Share Posted December 9, 2009 I'm kinda unsure of the actual question here! selection of table from MYSQL to be a php variable. The table used is the same one in the SQL statement! Quote Link to comment https://forums.phpfreaks.com/topic/184485-make-mysql-table-a-php-variable/#findComment-973877 Share on other sites More sharing options...
Scruffy Posted December 9, 2009 Author Share Posted December 9, 2009 I have a large database with multiple tables in it. I would like to use a variable from the current page URL to select the correct table from the database. I'm not sure if that helps explain it or not. Sorry I'm terrible at explaining coding. Quote Link to comment https://forums.phpfreaks.com/topic/184485-make-mysql-table-a-php-variable/#findComment-973878 Share on other sites More sharing options...
roopurt18 Posted December 9, 2009 Share Posted December 9, 2009 <?php $table_name = array_key_exists( 'tablename', $_GET ) ? $_GET['tablename'] : ''; if( ! preg_match( '/^[a-zA-Z0-9_]+$/', $table_name ) ) throw new Exception( '$table_name is invalid: ' . var_export( $table_name, true ) ); $select_stmt = " SELECT * FROM `{$table_name}` WHERE 1=1 "; ?> Quote Link to comment https://forums.phpfreaks.com/topic/184485-make-mysql-table-a-php-variable/#findComment-973914 Share on other sites More sharing options...
Scruffy Posted December 9, 2009 Author Share Posted December 9, 2009 Thanks! It seems to work! Quote Link to comment https://forums.phpfreaks.com/topic/184485-make-mysql-table-a-php-variable/#findComment-973915 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.