如何用这样的php变量检查数据库?
我有
$check = "1234567890";
这是表:check_data
_________ ______________________________________________________________
| id | key_pass |
|_________|______________________________________________________________|
|____1____|_______________1234567890abcdefghij___________________________|
|____2____|_______________6545ryu76543werfdt54___________________________|
|____3____|_______________345jfuryt75yrhtufkgo___________________________|
|____4____|_______________weoiufoiweu9ew8ew8w8___________________________|
|____5____|_______________oi34ioruiofuefiusdfo___________________________|
|____6____|_______________iuyiuysdifuysfiuyfds___________________________|
我想这样检查一下。
$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";}
我该怎么做?
答案 0 :(得分:5)
检查MySQL手册,您正在寻找SUBSTRING
功能。
$sql = "SELECT * FROM check_data WHERE SUBSTRING(key_pass, 1, 10) = '" . mysql_real_escape_string($check) . "';
答案 1 :(得分:4)
尝试 LEFT
LEFT(key_pass , 10);
像
$sql = "SELECT * FROM check_data WHERE LEFT(key_pass , 10) = '$check'";
您也可以使用 SUBSTRING
$sql = "SELECT * FROM check_data WHERE SUBSTRING(key_pass ,1, 10) = '$check'";
答案 2 :(得分:2)
你需要的功能是LIKE,
$sql = "SELECT * FROM check_data WHERE key_pass like ('$check%')";
你也可以
$sql = "SELECT * FROM check_data WHERE SUBSTR(key_pass,1,10) = '$check%'";
答案 3 :(得分:1)
您可以使用LEFT()
这样的功能:
ql = "SELECT * FROM check_data WHERE left(key_pass,10) = '$check'";
有一整套字符串函数可能很有用。检查MySQL reference
答案 4 :(得分:0)
$sql = "SELECT * FROM check_data WHERE SUBSTRING(key_pass, 1, 10)= '$check'";
答案 5 :(得分:0)
$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";}
答案 6 :(得分:0)
试试这个
"SELECT * FROM check_data WHERE SUBSTR(key_pass,1,10) = ".$check
或
"SELECT * FROM check_data WHERE key_pass LIKE '".$check."%'"