Reputation: 39
How to check database with php variables like this ?
I have
$check = "1234567890";
This is table : check_data
_________ ______________________________________________________________
| id | key_pass |
|_________|______________________________________________________________|
|____1____|_______________1234567890abcdefghij___________________________|
|____2____|_______________6545ryu76543werfdt54___________________________|
|____3____|_______________345jfuryt75yrhtufkgo___________________________|
|____4____|_______________weoiufoiweu9ew8ew8w8___________________________|
|____5____|_______________oi34ioruiofuefiusdfo___________________________|
|____6____|_______________iuyiuysdifuysfiuyfds___________________________|
i want to check like this.
$sql = "SELECT * FROM check_data WHERE key_pass(first char to ten char) = '$check'";
$query = mysql_query($sql);
$result = mysql_fetch_array($query);
if($result)
{echo "found";}
else
{echo "not found";}
How can i do that ?
Upvotes: 0
Views: 92
Reputation: 1556
the function you need is LIKE,
$sql = "SELECT * FROM check_data WHERE key_pass like ('$check%')";
you could also do
$sql = "SELECT * FROM check_data WHERE SUBSTR(key_pass,1,10) = '$check%'";
Upvotes: 2
Reputation: 1
Try this
"SELECT * FROM check_data WHERE SUBSTR(key_pass,1,10) = ".$check
or
"SELECT * FROM check_data WHERE key_pass LIKE '".$check."%'"
Upvotes: 0
Reputation: 164
$sql = "SELECT * FROM check_data WHERE key_pass LIKE % $check %";
$query = mysql_query($sql);
$result = mysql_fetch_array($query);
if($result)
{echo "found";}
else
{echo "not found";}
Upvotes: 0
Reputation:
You can use a LEFT()
function like this:
ql = "SELECT * FROM check_data WHERE left(key_pass,10) = '$check'";
There are a whole family of string functions that can be useful. Check the MySQL reference
Upvotes: 1
Reputation: 18600
$sql = "SELECT * FROM check_data WHERE SUBSTRING(key_pass, 1, 10)= '$check'";
Upvotes: 0
Reputation: 27082
Check MySQL manual, you're looking for SUBSTRING
function.
$sql = "SELECT * FROM check_data WHERE SUBSTRING(key_pass, 1, 10) = '" . mysql_real_escape_string($check) . "';
Upvotes: 5