Jump to content

[SOLVED] MySQL query to find table name exactly equal to input


ArizonaJohn

Recommended Posts

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());

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?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.