Ar Ad Ali Soomro Ar Ad Ali Soomro - 1 year ago 56
MySQL Question

Checking function with spaces in it in php

I am using this query

"SELECT * FROM items WHERE itemname LIKE '%$name%'"

Now if the variable value of $name="alex" then is gives correct information. But if I enter value $name="alex " along with spaces in it then it does not give the results and returns 0 rows.

if I enter $name="alex dude" then this is a valid name but $name="alex dude " this is not valid. I don't want to remove all whitespaces but at the end of the string only

I have written a function to clear out spaces at the end of name. This is the function.

function checkname($dataname)
$func_name ="";
$checker = substr($dataname, -1);
if($checker == " ")
$func_name = substr_replace($dataname, "", -1)

function checkname($dataname);
$dataname = $func_name;
return $dataname;

This gives me error that is PHP Parse error: syntax error, unexpected 'function' (T_FUNCTION) in C:\inetpub\vhosts\httpdocs\compare.php on line 176.

I don't understand why recursively calling function giving me error.

Can you guys help out with this and if there is better solution or better sql query than I am using, please help me!

Answer Source


SELECT * FROM items WHERE itemname LIKE '%". trim($dataname) ."%'"

trim($string) Strip whitespace (or other characters) from the beginning and end of a string