Prepared syntax statement problem

I am trying to make some pattern matching using the LIKE clause with a prepared statement

$connection->prepare('select u.name,u.lastname,t.user_type 
                                    from users u 
                                    join business_users b on u.user_id=b.crid 
                                    join buz_usertype t on b.bus_user_type=t.type_id 
                                    where u.lastname like=?%')

It is for a search query…I get a syntax error though at like=‘?’(search query).After a search I made it seems that I must use a user variable.

Nonetheless I do not know how to do that using a prepared statement.
Help will be appreciated…tnanks

just remove that equals sign

WHERE lastname LIKE '%foo%'

I tried this:

  if($stmt = $connection->prepare('SELECT u.name,u.lastname,t.user_type 
                                    FROM users u 
                                    join business_users b on u.user_id=b.crid 
                                    join buz_usertype t on b.bus_user_type=t.type_id 
                                    WHERE u.lastname LIKE ?%'))

and I still get the same message…

check...right syntax to use near '%'

Try removing the %', and add the % when passing the search parameter in…

this is the solution

...WHERE u.lastname LIKE CONCAT(?, "%")'

[quote=“designtrooper, post:5, topic:200952, full:true”]
this is the solution [/quote]why?

why wouldn’t you insert the trailing wildcard into your actual string?

i mean, the wildcard is ~supposed~ to be part of the string

for example, to find both McDonald and MacDonald, your string would be

'M%cDonald'

so the use of wildcards is dependent very much on where they’re supposed to be inside the string

they don’t always have to come at the end

1 Like

In prepared statements we are dealing with placeholders and you cannot juxtapose a placeholder with other characters…

?%

The above is wrong.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.