ArizonaJohn Posted June 2, 2009 Share Posted June 2, 2009 Hello, For the code below, the variable "$find" is entered by the user. The query for "$result" will then show all tables whose titles contain whatever was entered in for "$find" by the user. So, if the user types in the letter "o", then $result returns all tables that contain the letter "o" in the title (i. e. "boston," "oklahoma"). I would like the query $result to only return tables whose names equal exactly the variable "$find." So for example, if the user types in "o", then only a table whose title is "o" would be returned, and not "boston", "oklahoma", etc. What query would I use to do this? Thanks in advance, John $find = strip_tags($find); $find = trim ($find); $find = strtolower($find); $find = mysql_real_escape_string($find); $find = htmlentities($find); $result=mysql_query("SHOW TABLES FROM sand2 LIKE '%$find%'") or die(mysql_error()); Quote Link to comment https://forums.phpfreaks.com/topic/160712-solved-mysql-query-to-find-table-name-exactly-equal-to-input/ Share on other sites More sharing options...
Alex Posted June 2, 2009 Share Posted June 2, 2009 $sql = 'SELECT * FROM `table` WHERE title="' . $find . '"'; Quote Link to comment https://forums.phpfreaks.com/topic/160712-solved-mysql-query-to-find-table-name-exactly-equal-to-input/#findComment-848163 Share on other sites More sharing options...
PFMaBiSmAd Posted June 2, 2009 Share Posted June 2, 2009 Just remove the wild-card characters % - $result=mysql_query("SHOW TABLES FROM sand2 LIKE '$find'") However, you should not be storing data in separate tables using the table name (what you are calling title) to distinguish which table to find the data in. That is bad design and makes it more difficult (and slower) to access any of the data or to do anything that uses more than one name at a time, as you have probably found by how much harder has been to write queries. Edit: And I just reviewed your other current thread - DON'T dynamically create tables for each topic. It will make doing everything concerning outputting any of the information extra complicated and incredibly SLOW. Edit2: And if you did find a GOOD reason to dynamically create tables for each topic, it would also be your responsibility to maintain a list (a separate table) of the names that have been used and to prevent the creation of duplicates. What does your code do now when a duplicate is created? Quote Link to comment https://forums.phpfreaks.com/topic/160712-solved-mysql-query-to-find-table-name-exactly-equal-to-input/#findComment-848174 Share on other sites More sharing options...
ArizonaJohn Posted June 2, 2009 Author Share Posted June 2, 2009 Thanks. I'm using "UNIQUE" in the "TABLE CREATE" query. Quote Link to comment https://forums.phpfreaks.com/topic/160712-solved-mysql-query-to-find-table-name-exactly-equal-to-input/#findComment-848189 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.