我有一个像'迪拜,沙迦'这样的字符串,我想把它变成'迪拜','沙迦',
我使用ajax和my code
$city=$_GET['city'];
$sql = "SELECT * FROM filter where isdeleted = 0 ";
if ($city !="" && $city !="Empty" ){
$sql.=" and twon in ('".$citydata."')";
}
当我打印查询时,就像这样
SELECT * FROM filter where isdeleted = 0 and twon in ('dubai,sharjah,')
但我想这样
SELECT * FROM filter where isdeleted = 0 and twon in ('dubai','sharja')
任何人都可以使用php指导我如何做到这一点。
答案 0 :(得分:4)
以下是我接近它的方法。我将使用PDO而不是 mysqli ,因为尝试将数组放入mysqli_stmt::bind_param
只是一种痛苦。
首先,创建一个城市数组,删除所有空值
$params = array_filter(explode(',', $city), function($c) {
return !empty($c) && $c !== 'Empty';
});
$paramCount = count($params);
$query = 'SELECT * FROM filter where isdeleted = 0';
现在为准备好的语句生成一个占位符字符串。
if ($paramCount) {
$placeholders = implode(',', array_fill(0, $paramCount, '?');
// looks something like '?,?'
$query .= " AND twon IN ($placeholders)";
}
现在,准备一份声明
// assuming you have a PDO instance in $pdo created with something like
// $pdo = new PDO('mysql:host=localhost;dbname=your_db;charset=utf8', 'username', 'password', [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]);
$stmt = $pdo->prepare($query);
执行并获取值:)
$stmt->execute($params);
$filters = $stmt->fetchAll(PDO::FETCH_ASSOC);
答案 1 :(得分:3)
$cities = explode(",", $_GET['city']);
//escape!
foreach ($cities as $citykey => $city) {
$cities[$citykey] = "'" . mysql_real_escape_string($city) . "'";
}
$sql = "SELECT * FROM `filter` where `isdeleted` = 0";
if (!empty($cities)) {
$sql .= ' and `twon` in (' . implode(',', $cities) . ')';
}
答案 2 :(得分:2)
另一种方法是使用FIND_IN_SET()
。无需更改PHP代码。
$sql.=" and FIND_IN_SET(town, '".$citydata."')";
答案 3 :(得分:1)
你必须在这里爆炸和崩溃。休息是最后字符串中额外,
的问题。
$str = "dubai,sharjah,";
$citydata = implode("','",explode(',',rtrim($str,',')));
echo "'$citydata'";
答案 4 :(得分:1)
$sql.=" and twon in ('".str_replace(",","','",$citydata)."')";
答案 5 :(得分:1)
你可以尝试爆炸字符串
$cityparts = explode(",",$city);
你可以使用
$cityparts[0] and $cityparts[1] in your query
array explode ( string $delimiter , string $string [, int $limit ] )
您可以在[http://www.php.net/explode]
上找到更多信息希望这会有所帮助!!
答案 6 :(得分:0)
你可以这样做。
$string = 'dubai,sharjah';
$cities = explode(',', $string);
echo $cities[0]; //dubai
echo $cities[1]; //sharjah
答案 7 :(得分:0)
试试这个
$str = "dubai,sharjah,";
$arr = explode(",", $str);
$arr_temp = array()
foreach($arr as $s)
{
if($s!="")
{
$arr_temp[] = "'".$s."'";
}
}
$new_str = implode(",", $arr_temp);
echo $new_str; // output 'dubai','sharjah'
现在你的Sql将是
$sql = "SELECT * FROM filter where isdeleted = 0 and twon in ($new_str) ";
答案 8 :(得分:0)
你可以使用它
在$city
我修复了两个值,您可以在$_GET
;
$city = "dubai,sharjah,";
$query_string ="";
$words = explode(",",$city);
for($i=0;$i<count($words);$i++){$query_string .= "'".$words[$i]."',";}
$query_string = substr($query_string,0,strlen($query_string)-4);
然后使用您的查询
SELECT * FROM filter where isdeleted = 0 and twon in ($query_string)
答案 9 :(得分:-2)
if ($city !="" && $city !="Empty" )
{
$city_exp = explode(',' $city);
$sql .= " and twon in ('".implode("', '", $city_exp)."')";
}
我们在这里基本上做的是使用explode
将两个值放在一个数组中,然后使用implode