Jump to content

[SOLVED] Php mysql query with variable


erdomester

Recommended Posts

Hello,

I have a statement like this:

 

$sql_s = "SHOW TABLES FROM bkv45 WHERE Tables_in_bkv45 LIKE '%$search%' ";

 

I have tables like this:

B15_MW_something

B36_MWS_anything

B156_MW_notinteresting

....etc.

 

Let's say $search=B15.

The result of the query will be two tables: B15_something and B156_notinteresting.

BUT i only wan't B15_something. How am i supposed to do that?

I have tried

LIKE '%$search%_'  ->> error

LIKE '%$search_%'  ->> does nothing.

 

 

Link to comment
https://forums.phpfreaks.com/topic/159079-solved-php-mysql-query-with-variable/
Share on other sites

SQL is a ligical search engine and the wild cards you are using will return anything that pattern matches what was input.

 

If you do not want the B156_ tables, then with the B15 input, add the '_' to the string literal: $blabla=$search.'_';

 

Then the $sql= ... WHERE Tables_in_bkv45 LIKE '$blabla%' ";

 

will retrieve only tables that have strin patterns of B15_xxxxx.

NOT SOLVED!!!! I accidentally clicked on the button.

 

So.

Not working what u suggest.

Not working either:

CONCAT('%', $search, '_', '%')

This results in an error:

Error in query: SHOW TABLES FROM bkv45 WHERE Tables_in_bkv45 LIKE CONCAT('%', b206, '_', '%')

Unknown column 'b206' in 'where clause'

(b206 is the first table in the solution, nothing wrong with it, the code just somehow goes wrong)

 

either this:

LIKE '%".$search."_%'

 

or this:

LIKE CONCAT('%', '$search', '_', '%')

 

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.