php+sql2000 problem, array +sql query condition

Source: Internet
Author: User
PHP SQL Server

$WD 1=$_post[' wd1 ']; It turns out that.
Sql= "select * from xxx where pr_id like '%". $WD 1. " %'";
//**********************************************************
Now we're going to do it. Input: 123 King
Sql= "select * from xxx where pr_id like '%123% ' and the name like '% King '";
And so on, like above, the user enters a condition that may be multiple: 123 Wang Nan or a
wd1=$_post[' Wd1 ']; Now receive input data,
$split _dir = Split (' [] ', $WD 1);//I want to convert an array
Then the SQL server2000 statement does not know how to write, do not know what I mean to express clearly no, I want to express, ask for answers to ideas,
Sql= "??? ";


Reply to discussion (solution)

Sincerely, will not AH. Ask for advice. Thank you in advance!!

$where = "1=1"; $arr = Array (' col1 ', ' col2 ', ' col3 '); foreach ($split _dir as $key = = $val) {$where. = "and". $arr [$key]. " Like '% '. $val. " %' ";}

$arr is the name of the field that needs to appear in the where, of course, the security aspect of your own control.

This is only a general idea, this is obviously not perfect, because others may enter 123 male king or increase the space between the input, this need you to control

Well.. Thanks, I'll try.

Hey, well ... That's what I wrote.
$WD 1=$_post[' wd1 '];
$split _dir = Split (' [] ', $WD 1);

$arr = Array (' Encode ', ' name ', ' category ', ' model ');
$where 2 = "";
for ($m =0; $m
if ($m = = (count ($split _dir)-1))
{
for ($n =0; $n

if ($n = = (count ($arr)-1)) {
$where 2= $where 2. $arr [$n]. "Like '%". $split _dir[$m]. " %'";
}else{
$where 2= $where 2. $arr [$n]. "Like '%". $split _dir[$m]. " % ' or ';
}

}
$where 2= $where 2. " ";
}else{
for ($n =0; $n

if ($n = = (count ($arr)-1)) {
$where 2= $where 2. $arr [$n]. "Like '%". $split _dir[$m]. " %'";
}else{
$where 2= $where 2. $arr [$n]. "Like '%". $split _dir[$m]. " % ' or ';
}

}
$where 2= $where 2. ") and (";
}
}

  • 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.