I would like to enter all records in the database without hyphens and have the script strip the query of the hyphens before it queries the database. At least, that is how I think it could work.
I found some code online and wonder if it would do the trick, or if it might create problems:
Select REPLACE(PART_FIELD_NAME, ‘-’, ‘’)
At the moment I have a line in the script as follows:
$sql = “SELECT Image, Chain, Country, City, Top as ‘’, Medium as ‘’, Low as ‘’ FROM Chains WHERE Country LIKE ? OR City LIKE ?”;
What would I have to change? My knowledge of php is “zero”!
Thank you. How would I change the line above, or is it supposed to go on its own, and where?
$sql = “SELECT Image, Chain, Country, City, Top as ‘’, Medium as ‘’, Low as ‘’ FROM Chains WHERE Country LIKE ? OR City LIKE ?”; // Your query string
$prepare = $mysqli->prepare($sql); // Prepare your query string
$prepare->bind_param(‘ss’, $searchq, $searchq); // Bind the placeholders to your search variables
// s = string | i = integer | d = double | b = blob
$prepare->execute(); // Execute the prepared statement
$prepare->store_result(); // Store the results for later checking
I’m afraid, either I did something wrong, or it doesn’t work
There is a record “Figueira da Foz”. Just to test, I entered “Figueira-da-Foz” and it returned “Nothing found”
$sql = “SELECT Image, Chain, Country, City, Top as ‘’, Medium as ‘’, Low as ‘’ FROM Chains WHERE Country LIKE ? OR City LIKE ?”; // Your query string
$prepare = $mysqli->prepare($sql); // Prepare your query string
$searchq = str_replace( ‘-’, ‘’, $searchq );
$prepare->bind_param(‘ss’, $searchq, $searchq); // Bind the placeholders to your search variables
// s = string | i = integer | d = double | b = blob
$prepare->execute(); // Execute the prepared statement
EDIT
Wait! I’m not fully awake yet… I mus put $City somewhere… Where? Or does it apply to all fields?
Assuming that code was to substitute the one you gave me before in the same place, it does not work and the page does not go through returning instead on a white background
Before: %figueira-da-foz%
After: %figueiradafoz%
It does not work but it is strange because before your code and with your code if I search for Paris, not only I get Paris, but also “Villeparisis” beacuse of the double %%. So, why don’t I get Figueira da Foz with figueiradafoz? After all, I get it with just Figueira or even figue
The script I supplied was to echo on the screen the search variable before and after applying the str_replace(); function results and then to stop the remaining script.
Can you see the problem?
Once you have rectified the problem remove the echo and die statements;
You should not give up, only take a rest and approach the problem with new vigour
Your Post #7
It does not work but it is strange because before your code and with your code if I search for Paris, not only I get Paris, but also “Villeparisis” beacuse of the double %%. So, why don’t I get Figueira da Foz with figueiradafoz? After all, I get it with just Figueira or even figue
The script I supplied would have shown the following results:
Before: Figueira-da-Foz
After: FigueiradaFoz
// The second str_replace() parameter should be changed to a single space and not an empty space:
$searchq = str_replace( '-', ' ', $searchq);
// New results:
Before: Figueira-da-Foz
After: Figueira da Foz
If there is still a problem try removing trailing spaces which are not shown but can effect the search query:
This will strip the hyphens from both the country and city fields you are searching for
$sql = "SELECT Image, Chain, Country, City, Top as '', Medium as '', Low as '' FROM Chains WHERE Country LIKE ? OR City LIKE ?";
$prepare = $mysqli->prepare($sql);
$prepare->bind_param('ss', str_replace( '-', '', $country), str_replace( '-', '', $city));
$prepare->execute();
Hi @qim
The original script I supplied assumed either a leading or trailing hyphen, unfortunately your $searchq hyphen was between words. The debugging script highlighted the missing spaces.
No doubt you have learnt something new about Php rather than just copying and pasting supplied solutions