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
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

Link to comment
Share on other sites

 

 

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
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.