Jump to content

Regular expressions in MySQL


DanielW

Recommended Posts

When I search my database I can only get results returned back if I search for only a single letter, when I type a whole word I get 0 matches returned.

I have the following if statment where I have been trying to get the correct regular expressions but I am unsure where I am going wrong, What expression would I need to use to be able to type in a city name for example Tucson and get a result?

if($_POST['filter1'] == "City")
	{		
		$sqlCommand = "SELECT city_id AS id, city_name AS title FROM city WHERE city_name LIKE '%$searchquery%'";
		//$sqlCommand = "SELECT city_id AS id, city_name AS title FROM city WHERE MATCH (city_name) AGAINST ('searchquery')";
		//$sqlCommand = "SELECT city_id AS id, city_name AS title FROM city WHERE city_name REGEXP  '%$searchquery%'";
	} 
Link to comment
https://forums.phpfreaks.com/topic/288020-regular-expressions-in-mysql/
Share on other sites

Hi thanks for taking some time to look at my code

$searchquersy gets its value from this line

$searchquery = preg_replace('#[^a-z 0-9?!]#i', '', $_POST['searchquery']);

the value of $sqlCommand when it is being run i not sure I have tried to see in the chrome developer consle but I cannot find what it is, i am pretty new to php

 

 

the value of $sqlCommand when it is being run i not sure I have tried to see in the chrome developer consle but I cannot find what it is, i am pretty new to php

PHP code never reaches the browser. So the console wont be able to tell you the value of $sqlCommand.

 

Instead you echo $sqlCommand in your code.

$sqlCommand = "SELECT city_id AS id, city_name AS title FROM city WHERE city_name LIKE '%$searchquery%'";
echo $sqlCommand; // output the SQL query

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.