I have this variable that get from a POST command [passing thru the SESSION] --> $ark = 123456-78
I would like to keep eveything before "-" so 123456 to make my research.
I tought that using Subtring will work so i tried this :
This is my request :
$req = "SELECT COUNT(`Home`) FROM `users` WHERE `Home` LIKE('$ark')";
What I tried with no excpected results:
$req = "SELECT COUNT(`Home`) FROM `users` WHERE `Home` LIKE FLOOR('$ark')";
and
$req = "SELECT COUNT(`Home`) FROM `users` WHERE `Home` LIKE SUBSTRING('$ark',6)"
Any idea where I made a mistake ?
Edit: Finally I found the solution using SUBSTRING_INDEX()
$req = "SELECT COUNT(`Home`)FROM `users` WHERE `Home` LIKE SUBSTRING_INDEX('%$ark%','-','1')";
Thank you all for your help Rflow
Please try this one. Hope it should worked.
$req = "SELECT COUNT(`Home`) FROM `users` WHERE `Home` LIKE concat('%', SUBSTRING('$ark',6), '%')";
You need %
as wildcard
SELECT COUNT(`Home`)
FROM `users`
WHERE `Home` LIKE concat('%', SUBSTRING('$ark',6), '%')
You sould use concat for build the proper like with wildchar
$req = "SELECT COUNT(`Home`) FROM `users` WHERE `Home` LIKE concat('%', '$ark' , '%')";
If LIKE clause is provided without a wildcard character it simply works like equal to operator.
So when you trywhere home LIKE SUBSTRING('$ark',6)
, is nothing but same aswhere home = '123456'
which is not true in your case.
So you have to use wildcard characters to match your datawhere home LIKE concat('%', SUBSTRING('$ark',6), '%')
(you can skip first % if you know that 123456 is not in between) which is equal towhere home LIKE '%123456%'