亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

新手小白,如下這段代碼,想用于搜索,但不知道該如何添加mysql查詢語句,使之能循環查詢遍歷后的值?

新手小白,如下這段代碼,想用于搜索,但不知道該如何添加mysql查詢語句,使之能循環查詢遍歷后的值?

PHP
慕娘9325324 2022-11-20 13:13:00
function strsToArray($strs) { $result = array(); $array = array(); $strs = str_replace(',', ',', $strs); $strs = str_replace("n", ',', $strs); $strs = str_replace("rn", ',', $strs); $strs = str_replace(' ', ',', $strs); $array = explode(',', $strs); foreach ($array as $key => $value) {if ('' != ($value = trim($value))) { $result[] = $value;echo $value; }}return $result; } //test $strs = $_GET["zi"];var_dump(strsToArray($strs));
查看完整描述

3 回答

?
牧羊人nacy

TA貢獻1862條經驗 獲得超7個贊

<?php

function strsToArray($strs) { 
$result = array(); 
$array = array(); 
$strs = str_replace(',', ',', $strs); 
$strs = str_replace("n", ',', $strs); 
$strs = str_replace("rn", ',', $strs); 
$strs = str_replace(' ', ',', $strs); 
$array = explode(',', $strs); 
foreach ($array as $key => $value) {
if ('' != ($value = trim($value))) { 
$result[] = $value;
}
}
foreach($result as $k=>$v){
$sql="";
$sql="select * from table where 查詢字段 = '".$v."' ";
$row = mysql_query($sql);
if($ret = mysql_fetch_assoc($row)){
print_r($ret);
}else{
echo "沒有找到值為".$v."的數據";
}
echo "<br>";
}


$strs = $_GET["zi"];
strsToArray($strs);

?>


查看完整回答
反對 回復 2022-11-24
?
紅顏莎娜

TA貢獻1842條經驗 獲得超13個贊

//示例代碼:index.php


<?php//請求URL示例:http://localhost/index.php?zi=111,zz,ddd//獲取參數$strs $_GET["zi"];//調用函數(strsToArray) 構造查詢sql條件$where = strsToArray($strs);//連接數據庫$con = mysql_connect("localhost","root","root");if (!$con)  {  die('Could not connect: ' . mysql_error());  } mysql_select_db("my_db"$con);//拼裝sql、結果如:SELECT * FROM test where 1=1 and title like '%111%' and title like '%zz%' and title like '%ddd%' $sql="SELECT * FROM test where 1=1 ".$where;echo $sql;exit;$result = mysql_query($sql); echo "查詢信息如下:";while($row = mysql_fetch_array($result))  {  echo $row['字段2'] . "=====" $row['字段三'];  echo "<br />";  } mysql_close($con);function strsToArray($strs) {$where "";$array array();$strs str_replace(','','$strs);$strs str_replace("n"','$strs);$strs str_replace("rn"','$strs);$strs str_replace(' '','$strs);$array explode(','$strs);foreach ($array as $key => $value) {    if ('' != ($value = trim($value))) {        $where.=" and title like '%{$value}%' ";    }}return $where;}?>


查看完整回答
反對 回復 2022-11-24
?
炎炎設計

TA貢獻1808條經驗 獲得超4個贊

首先連接數據庫


$conn = mysql_connect('localhost''root''mypassword'); //連接數據庫mysql_select_db('mydatabase'); //選擇庫mysql_query('set names mycharset'); //設置編碼

調用函數得到關鍵字數組


$arr = strsToArray($strs); //得到要查詢的關鍵字數組

遍歷查詢


$result array(); //初始化結果數組foreach($arr as $keyword//遍歷數組{  $sql "select * from mytable where myfield like '%$keyword%'"//構造SQL語句  $obj = mysql_query($sql); //查詢數據庫  $result array_merge($result, mysql_fetch_array($obj)); //取得結果數組}$result array_unique($result); //去重


 


查看完整回答
反對 回復 2022-11-24
  • 3 回答
  • 0 關注
  • 118 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號