2

I have a table with people holidays 'id', 'day', 'month', 'name'

<table rules='all' border='black'>
        <tr>
            <td>id</td>
            <td>day</td>
            <td>month</td>
            <td>name</td>
        </tr>
        <tr>
            <td>1</td>
            <td>1</td>
            <td>5</td>
            <td>Adam</td>
        </tr>
        <tr>
            <td>2</td>
            <td>2</td>
            <td>5</td>
            <td>Pavel a Petr</td>
        </tr>
</table>
I'm saving name from people holidays into strtolower($name) => $name = Pavel a Petr

And I have a table of users where name is Pavel

Now I need SQL query which can select * from users where lower(name) contains '".$name."'

I'm looking for SQL solution. If there is no SQL solution, can some one suggest me a php solution.

Thank you.

Nikhil Vartak
  • 5,002
  • 3
  • 26
  • 32
koca79331
  • 321
  • 4
  • 19

6 Answers6

3

Try to use MySQL LIKE Clause

E.g:

"select * from `users` where lower(name) LIKE '%$name%'"

If SQL LIKE clause is used along with % characters, then it will work like a meta character (*) in UNIX while listing out all the files or directories at command prompt.

Yatin Khullar
  • 1,580
  • 1
  • 12
  • 26
1

You probably want to use LIKE to pattern match.

SELECT * FROM users WHERE lower(name) LIKE '%$name%';

The % is the wildcard operator, so this allows anything before or after the name.

Nicolas Gervais
  • 33,817
  • 13
  • 115
  • 143
lkjaero
  • 31
  • 6
0

you can use REGEXP

SELECT * FROM `users` WHERE LOWER(name) RLIKE  "[[:<:]]$name[[:>:]]"
Dyrandz Famador
  • 4,499
  • 5
  • 25
  • 40
0

Maybe it is this you are looking for:

EDIT: You should not have a user with the name "a" :-)

EDIT2: Just found, that the sql-server tag is taken away...

DECLARE @peopleHolidays TABLE(id INT,day INT,month INT, name VARCHAR(100));
INSERT INTO @peopleHolidays VALUES(1,1,5,'Adam'),(2,2,5,'Pavel a Petr'),(2,3,5,'Adam a Max'),(2,4,5,'Max a Petr');

DECLARE @users TABLE(id INT IDENTITY,name VARCHAR(100));
INSERT INTO @users VALUES('Adam'),('Pavel'),('Petr'),('Max');

SELECT *
FROM @peopleHolidays AS pH
CROSS APPLY(SELECT CAST('<r>' + REPLACE(pH.name,' ','</r><r>') + '</r>' AS XML)) AS AsXml(x)
CROSS APPLY
(
    SELECT * FROM @users AS u
    WHERE u.name IN
    (
        SELECT a.b.value('.','varchar(max)')
        FROM AsXml.x.nodes('/r') AS a(b)
    )
) AS UsersInHoliday
Shnugo
  • 66,100
  • 9
  • 53
  • 114
0

As other posters have said, LIKE will give you a comparator for part of the string. You can use string concatenation for the like argument and therefore pass the string you're searching for safely and not at as much risk of SQL Injection

select * from users where name like '%' + :name + '%'

Use prepared statements in PHP;

How can I prevent SQL-injection in PHP?

PDO::prepare

Community
  • 1
  • 1
Edward Comeau
  • 3,874
  • 2
  • 21
  • 24
0

So I solved this by PHP, not by SQL, code just looks like

function isClientInDb($name, $array){
        global $db;

        $name = strtolower($name);      

        $query = "SELECT * from `clients` where LOWER (name) = '".$name."' and `uid`='".$_SESSION['uid']."'";
        $result = $db->select($query);      
        if(count($result) < 1){

        if(intval(preg_match('/\s/',$name)) > 0){
            if(strpos($name, ",")){
                $pos = strpos($name, ",");      
                $minus = 1; 
            }else{
                if(strpos($name, "a")){
                $pos = strpos($name, "a");  
                $minus = 0;         
                }else{
                $pos = strpos($name, " ");
            }           
            }           
            $pole = array(strtolower(substr($name,0,$pos)), strtolower(substr($name,-($pos+$minus))));

            $sql = "lower(name) = '".$pole[0]."' or lower(name) = '".$pole[1]."'";              
            }else{
            $sql = "lower(name) LIKE '%".$name."%'";            
            }       

        $query = "SELECT * from `clients` where ( $sql ) and `uid`='".$_SESSION['uid']."'";
        $result = $db->select($query);
        if(count($result) < 1){
            return false;           
                        }else{
                            return true;
                        }


        }else{
        return true;        
        }   
        }   
koca79331
  • 321
  • 4
  • 19