PHP: LIKE after space? - php

Right now i have a column called full_name where the first and last name gets stored. It is seperated by a normal space, so a name is stored like this: "Firstname Lastname". Now when im making this search users function, i have this:
$query = "SELECT full_name, id, user_name, sex, last_access, bostadsort FROM users WHERE full_name LIKE '$searchUser%'";
It works great by finding if you search by the firstname. I wish to make if you e.g type "lastname" then the user also will come up.
Now i find this quite my fault, that i started by having 1 column for the full_name and not firstname lastname columns.
So i wonder if i can do this without making two new columns and change rest of my code to work with firstname lastname new columns.. ?
Im using MySQL

full_name LIKE '$name%' OR full_name LIKE '% $name'
You should however split this column into two separate columns. What about multiple forenames/surnames?

My recommendation is to get them in 2 separate fields. Otherwise use % wildcard as well in the beginning of the string.

... WHERE full_name LIKE '$searchUser%' OR full_name LIKE '%$searchUser' OR full_name LIKE '%$searchUser%'
will that do?

Put a % in front of your variable to denote you want to match items at the end, a % in the back to denote you want to match the items in the front, and a % in front and back to denote the item you want to match is in the middle.

Related

Search a MySQL table for Comma Separated Area Codes

I'm trying to modify an existing script I have, that will only output rows that contain specific area codes to users that have be assigned the specific area codes.
Example:
A column named designed_areas in the users table will contain different data like: CM,SS,RH
When a lead comes in, I'm using substr to detect the first 2 characters of a post code.
$trimmed = substr("$postcode", 0, 2);
$viewLeads=mysqli_query($con,"SELECT * FROM leads WHERE team = '$getID->team' ORDER BY id DESC");
while($lead=mysqli_fetch_object($viewLeads)){
I'd like to expand on the mysqli_query and select all the leads from the database where the first 2 characters of the postcode matches any of users designated_areas.. which are comma seperated.
This way, he'll only see leads with the areas he can work on.
Can anyone help?
You can try to use a query like:
SELECT * FROM leads WHERE team = '$getID->team' and designed like '%{$trimmed}%' ORDER BY id DESC;
Php code:
$viewLeads=mysqli_query($con,"SELECT * FROM leads WHERE team = '$getID->team' and designed like '%{$trimmed}%' ORDER BY id DESC;");
You can do it with a regex. For example, in MySQL, if your postal_code table looks something like this:
PersonID Zips
1 11111,22222,33333
2 22222,12121,32323
And you want persons that have postal codes like 33*
SELECT PersonID FROM `postal_code` WHERE zips rlike '33[0-9]{3}'
This assumes that all postal codes are 5 digits and you're always searching using 2 digits. It basically says "find me 5 digit numbers where the first two digits are 33" or whatever your search numbers are.
The PHP code might look like:
$search = 33 ;
$sql = "SELECT PersonID FROM `postal_code` WHERE zips rlike '$search[0-9]{3}'" ;
This solution will find matches regardless of commas, spaces, etc. You won't need any further processing in your PHP code.

How do I get first name, middle name and last name as username with a dot in between the names and a number at the end for a unique username?

I'm working on a project, but I would like to know how I get the first name, middle name and last name as the username with a dot in between their full names and a number at the end for a unique username? In MySQL. Just like Facebook's system. I also want so if the middle name is NULL, then it doesn't like a space in the middle.
I have tried a lot of different things, but nothing worked yet.
SELECT COALESCE(CONCAT(first_name, '.', COALESCE(CONCAT(middle_name, '.', last_name),
first_name,middle_name,last_name)), middle_name,'.', COALESCE(CONCAT(first_name, '.',
last_name),first_name,last_name)) AS username FROM users
That is how close I have come, but it has a problem if they don't have a middle name, it will display two dots instead of one. But I still need something to get the first name, middle name and last name as the username with a dot in between their full names and a number at the end for a unique username.
I don't know what you want for the number at the end, but for the name, use concat_ws():
select concat_ws('.', first_name, middle_name, last_name)
If any of the values are NULL then the value is ignored (so f.l rather than f..l).
If you still want separators when the first/last name are NULL, then use coalesce():
select concat_ws('.', coalesce(first_name, ''), middle_name, coalesce(last_name, ''))
You can concat a number at the end using concat() or by adding it as an argument (in which case, there will be a dot before the number).
If you want numbers at the end of the username then you can use the id or SL value of the user. The ID or SL column should be NOT NULL, AUTO INCREMENT. You can use:
select concat_ws('.', first_name, middle_name, last_name, id)

MySQL using WHERE clause with multiple LIKEs

I have a simple table with names, some beginning with a letter and some beginning with a number. I am trying to filter out the names that start with a number, and only select the names that start with a letter of the alphabet.
Here are a few of my attempts, which have failed to produce the desired results:
SELECT name
from subjects
WHERE name LIKE ('A%') AND
WHERE name LIKE ('B%') AND
WHERE name LIKE ('C%')...;
SELECT name
from subjects
WHERE name LIKE ('A%', 'B%', 'C%', ...);
SELECT name
from subjects
WHERE name LIKE ('A%') AND ('B%') AND ('C%') ...;
Thank you, in advance, for your help. I appreciate it very much. Stack Overflow has been my savior so many times as I am learning to program with PHP and MySQL.
Lori
A simpler solution would be use regexp operator instead of passing multiple likes.
Try:
SELECT name from subjects WHERE name REGEXP '^[a-zA-Z]';
This will select all records with name starting with both lowercase and uppercase alphabets.
the problem you are having is you have included multiple wheres. Simply remove them
SELECT name
from subjects
WHERE name LIKE ('A%') AND
name LIKE ('B%') AND
name LIKE ('C%')...;
Try Regexp:
SELECT name
FROM subjects
WHERE name
REGEXP '[A-Za-z]'

MySQL Searching forename and surname

OK, I have a DB table that's called players and each player has a forename and surname. Then I have a PHP Ajax search thing that I call to search for players. For example... in the input box, someone types James and there's a row in the table with forename and surname James and Smith respetively.
I do this $check = mysql_query("SELECT * FROMplayersWHEREsurnameLIKE '%$name%' ORforenameLIKE '%$name%' LIMIT 0, 10") or die(mysql_error());
And it returns at least 10 with either forename or surname like the keyword James. However, if I type James Smith, despite it being in the table, I get zero results.
How do I fix this?
Are you using InnoDB or MyISAM? If your using MyISAM, you can create a single field which holds the combined name and then search it using a full text index. So lets imagine you add a new field called combined_names you would search it like this
SELECT * FROM table WHERE match(combined_names) against('John Smith');
This would find any row with either John or Smith in, you can change it to match only those rows with both parts you would add plusses like so:
SELECT * FROM table WHERE match(combined_names) against('+John +Smith');
Here is the documentation on the MySQL site where you can find out more:
http://dev.mysql.com/doc/refman/5.0/en/fulltext-search.html
SELECT * FROM players WHERE CONCAT(forename, ' ', surname) = '$name' OR forename LIKE '%$name%' OR surname LIKE '%$name%'
split the name up on spaces so the query runs twice (if there is one space)
the query will run for both names
$nameBits = explode($name," ");
run the query for each piece of $nameBits
surname LIKE '%$nameBits[$i]%'

Displaying searched record for a query using 'like' at the top in mysql

I am writing a search query which makes a search based on first name and last name.
The query is like this:
select fname,lname
from users
where fname like fname like '%$name%' or lname like '%$name%';
if the user enters first name and last name in the search box,I have exploded it with a 'space' and the query goes like this
select fname,lname
from users
where fname like fname like '%$f_name%' or lname like '%$l_name%';
In the second query if I enter John Thomas,it shows me all the records with John or Thomas in first or last name,but the actual search result i.e John Thomas(exact match) is somewhere below in the results.(If I have 100 results its on the 60th position).
How can I modify the query to achive this or do I have to handle it programatically? i.e Check the result array and match the check box value for its presence and display it fist.
Try:
SELECT fname, lname FROM users
WHERE (fname = '$f_name' AND lname = '$l_name')
OR (fname LIKE '%$f_name%') OR (lname LIKE '%$l_name%');
And if you need to order then ORDER BY lname, fname in the end of the query, depending on what you need to order by of course.
ADD in query
ORDER BY `fname`
You can testing mysql sorting
More information here ==> http://dev.mysql.com/doc/refman/5.0/en/sorting-rows.html

Categories