How to use like in an in sql statement

Garret Kaye

Hi I'm using PHP to try and retrieve data from a database while looping through an array inside the in statement like this.

$sql2 = "SELECT * FROM pixacour_pixacourt.UserCaseMessages WHERE FavorIDs IN  (" ;
    $count = 0;
foreach($followingArray as $value)
{
    if($count==count($followingArray)-1)
    {
        $sql2 .= "LIKE";
        $sql2 .= "%'".$value."'%";
    }
    else
    {
        $sql2 .= "LIKE";
        $sql2 .= "%'".$value."'%,";        
    }
    ++$count;
}
$sql2 .= ")";

I get this error, that says

"trying to get property of non object"

I can't figure out what is going on any suggestions would be much appreciated thank you for your time.

Tim

You should not be using LIKE in an IN clause but you do need to comma delimit elements. No need to keep track of the count, either. foreach will cease when the array has been traversed and you can trim off the trailing comma.

$sql2 = "SELECT * FROM pixacour_pixacourt.UserCaseMessages WHERE FavorIDs IN  (" ;
foreach($followingArray as $value)
{
        $sql2 .= "'".$value."', ";        
}
$sql2 = rtrim($sql2,',');
$sql2 .= ");";

If this fails, like Gordon says, echo $sql2 and the syntax error will probably be clear.

If you do indeed need to use LIKE and wildcard matching, you could append multiple OR clauses, one for each $value.

$sql2 = "SELECT * FROM pixacour_pixacourt.UserCaseMessages WHERE " ;
$whereClause = "";
foreach($followingArray as $value)
{
        $whereClause .= " OR FavorIDs LIKE '%".$value."%' ";        
}
$whereClause = ltrim($whereClause, " OR");
$sql2 .= $whereClause . ";";

UPDATE 1/9/15

I realized there's a bug in this code in that when $followingArray is empty, we'd receive a MySQL syntax error because the query ends with "WHERE". Here's a new version which resolves that bug:

$sql2 = "SELECT * FROM pixacour_pixacourt.UserCaseMessages" ;
if(count($followingArray) >= 1) {
    $sql2 .= " WHERE";
}
$whereClause = "";
foreach($followingArray as $value)
{
        $whereClause .= " OR FavorIDs LIKE '%".$value."%' ";        
}
$whereClause = ltrim($whereClause, " OR");
$sql2 .= $whereClause . ";";

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to use REGEXP_LIKE in a CASE statement?

From Dev

How to use if statement in SQL Query?

From Dev

How do I run an SQL update query using a like statement

From Dev

How to ignore non alphanumeric characters for SQL LIKE statement?

From Dev

How to use sql server like query?

From Dev

How to use SQL function LIKE properly

From Dev

How to use Parameter field in a record selection "like" statement?

From Dev

How to use LIKE statement with multiple values from another field?

From Dev

How to use multiple values with like in sql

From Dev

SQL how to prevent masking by like statement

From Dev

How to properly use SQL LIKE statement to query DB from Flask application

From Dev

how to use regular expression in MySQL where like and in statement in where clause

From Dev

How can I use the '%' wildcard with a LIKE statement in PDO SQL?

From Dev

How do I run an SQL update query using a like statement

From Dev

How can I use like in a list for SQL?

From Dev

SQL LIKE statement not working

From Dev

How to use sql server like query?

From Dev

How to use Parameter field in a record selection "like" statement?

From Dev

SQL Select Statement LIKE IN

From Dev

How to use LIKE statement with multiple values from another field?

From Dev

SQL: Use one Table as Input for a 'Like' Statement on Another Table

From Dev

How to use LIKE in dynamic SQL in a stored procedure?

From Dev

use of like statement with or in mysql

From Dev

NOT LIKE IN statement in SQL

From Dev

How to use nchar with like operator in SQL Server

From Dev

How to write sql 'like' statement in Laravel?

From Dev

How to use WhereBetween date with %LIKE% in sql query

From Dev

SQL LIKE: How to add string variable in SQL like statement?

From Dev

Not like statement in SQL not working

Related Related

  1. 1

    How to use REGEXP_LIKE in a CASE statement?

  2. 2

    How to use if statement in SQL Query?

  3. 3

    How do I run an SQL update query using a like statement

  4. 4

    How to ignore non alphanumeric characters for SQL LIKE statement?

  5. 5

    How to use sql server like query?

  6. 6

    How to use SQL function LIKE properly

  7. 7

    How to use Parameter field in a record selection "like" statement?

  8. 8

    How to use LIKE statement with multiple values from another field?

  9. 9

    How to use multiple values with like in sql

  10. 10

    SQL how to prevent masking by like statement

  11. 11

    How to properly use SQL LIKE statement to query DB from Flask application

  12. 12

    how to use regular expression in MySQL where like and in statement in where clause

  13. 13

    How can I use the '%' wildcard with a LIKE statement in PDO SQL?

  14. 14

    How do I run an SQL update query using a like statement

  15. 15

    How can I use like in a list for SQL?

  16. 16

    SQL LIKE statement not working

  17. 17

    How to use sql server like query?

  18. 18

    How to use Parameter field in a record selection "like" statement?

  19. 19

    SQL Select Statement LIKE IN

  20. 20

    How to use LIKE statement with multiple values from another field?

  21. 21

    SQL: Use one Table as Input for a 'Like' Statement on Another Table

  22. 22

    How to use LIKE in dynamic SQL in a stored procedure?

  23. 23

    use of like statement with or in mysql

  24. 24

    NOT LIKE IN statement in SQL

  25. 25

    How to use nchar with like operator in SQL Server

  26. 26

    How to write sql 'like' statement in Laravel?

  27. 27

    How to use WhereBetween date with %LIKE% in sql query

  28. 28

    SQL LIKE: How to add string variable in SQL like statement?

  29. 29

    Not like statement in SQL not working

HotTag

Archive