user3769723
user3769723

Reputation: 39

How to check database with php variables like this?

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

Answers (7)

Jacky Cheng
Jacky Cheng

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

grandcan
grandcan

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

karan
karan

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

user1864610
user1864610

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

Sadikhasan
Sadikhasan

Reputation: 18600

$sql = "SELECT * FROM check_data WHERE SUBSTRING(key_pass, 1, 10)= '$check'";

Upvotes: 0

pavel
pavel

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

GautamD31
GautamD31

Reputation: 28763

Try with LEFT

LEFT(key_pass , 10);

Like

$sql = "SELECT * FROM check_data WHERE LEFT(key_pass , 10) = '$check'";

Also you can use SUBSTRING like

$sql = "SELECT * FROM check_data WHERE SUBSTRING(key_pass ,1, 10) = '$check'";

Upvotes: 4

Related Questions