Novice for advice! A question about search

Source: Internet
Author: User
Self-study PHP, do the search box encountered a problem, when I entered the search string as long as there is no space at the beginning of anything can be, he will all the table all the data are displayed, did not understand what is the problem, the amount, the code is such
if (Isset ($_get[' Sousuok ')) {
if (Ctype_space ($_get[' Sousuok ')) {
echo "Illegal data! ";
}else{
$s =explode ("", $_get[' Sousuok ']);
Require_once '. /connvar.php ';
$dbc =mysqli_connect (db_host,db_user,db_password,db_name);
mysql_query ("SET NAMES ' UTF8 '");
$exec = "SELECT * from Cangku where name like '% $s [0]% ' or '% $s [1]% ';";
$result =mysqli_query ($DBC, $exec);
if (Mysqli_num_rows ($result) >0) {
while ($rs =mysqli_fetch_object ($result)) {
$name = $rs->name;
$tu = $rs->biaoqiantu;
echo "

". $name." "."

";
};
echo mysqli_num_rows ($result);
}else{
echo mysqli_num_rows ($result);
echo "Not Found! ";
};
Mysqli_free_result ();//Release memory
Mysqli_close ();//Disconnect the database
}
}

?>


Reply to discussion (solution)

$s =explode ("", $_get[' Sousuok ']);
If $_get[' Sousuok ' begins with a space, then $s [0] is an empty string
So $exec = "SELECT * from Cangku where name like '% $s [0]% ' or '% $s [1]% ';";
It becomes $exec = "SELECT * from Cangku where name is like ' percent ' or '% query word% ';";
The name like ' percent ' is always set up, and nature is the whole thing.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.