Heim >Backend-Entwicklung >PHP-Tutorial >sql出来的集合如何变换成想要的字符串 100分!
query('SELECT infotags,id FROM hd_ecms_shop_data_1 WHERE hd_ecms_shop_data_1.infotags LIKE "%我爱你%"')
想把上面这句query出来的id 组成一个字符串,不知怎么弄呢
比如 输出了五个id值,分别是 1,2,3,4,5
现在我想把这五个值通过变换,搞成这样的字符串
(1,2,3,4,5)
不知怎么弄呢
我想用下面的方法试图做一下,但是不行呢
$result=query('SELECT infotags,id FROM hd_ecms_shop_data_1 WHERE hd_ecms_shop_data_1.infotags LIKE "%我爱你%"');
$foodsPic=array();
while($row=mysql_fetch_array($result)){
$foodsPic[]=$row;
}
print_r($foodsPic);
上面这个方法不仅把id都弄进去了,把别的没用的也一鼓脑弄进去了,我只想要id的集合,并且通过处理,把这几个id搞成字符串的形式,即(1,2,3,4,5)
不知如何改进呢,
$SQ="SELECT infotags,id FROM hd_ecms_shop_data_1 WHERE hd_ecms_shop_data_1.infotags LIKE '%我爱你%'";$result=mysqli_query($link,$SQ);$idStr='';if(mysqli_num_rows($result)){ $data=mysqli_fetch_all($result,MYSQL_ASSOC); foreach($data as $value){ if(isset($value['id'])) $idStr.=$value['id']; } $idStr=rtrim($idStr,','); echo $idStr;}
$SQ="SELECT infotags,id FROM hd_ecms_shop_data_1 WHERE hd_ecms_shop_data_1.infotags LIKE '%我爱你%'";$result=mysqli_query($link,$SQ);$idStr='';if(mysqli_num_rows($result)){ $data=mysqli_fetch_all($result,MYSQL_ASSOC); foreach($data as $value){ if(isset($value['id'])) $idStr.=$value['id']; } $idStr=rtrim($idStr,','); echo $idStr;}
<?phpdefine('InEmpireCMSDbSql',TRUE);class mysqlquery{ var $dblink; var $sql;//sql语句执行结果 var $query;//sql语句 var $num;//返回记录数 var $r;//返回数组 var $id;//返回数据库id号 //执行mysql_query()语句 function query($query){ global $ecms_config; $this->sql=mysql_query($query,return_dblink($query)) or die($ecms_config['db']['showerror']==1?mysql_error().'<br>'.str_replace($GLOBALS['dbtbpre'],'***_',$query):'DbError'); return $this->sql; } //执行mysql_query()语句2 function query1($query){ $this->sql=mysql_query($query,return_dblink($query)); return $this->sql; } //执行mysql_query()语句(选择数据库USE) function usequery($query){ global $ecms_config; $this->sql=mysql_query($query,$GLOBALS['link']) or die($ecms_config['db']['showerror']==1?mysql_error().'<br>'.str_replace($GLOBALS['dbtbpre'],'***_',$query):'DbError'); if($GLOBALS['linkrd']) { mysql_query($query,$GLOBALS['linkrd']); } return $this->sql; } //执行mysql_query()语句(操作数据库) function updatesql($query){ global $ecms_config; $this->sql=mysql_query($query,return_dblink($query)) or die($ecms_config['db']['showerror']==1?mysql_error().'<br>'.str_replace($GLOBALS['dbtbpre'],'***_',$query):'DbError'); return $this->sql; } //执行mysql_fetch_array() function fetch($sql)//此方法的参数是$sql就是sql语句执行结果 { $this->r=mysql_fetch_array($sql); return $this->r; } //执行fetchone(mysql_fetch_array()) //此方法与fetch()的区别是:1、此方法的参数是$query就是sql语句 //2、此方法用于while(),for()数据库指针不会自动下移,而fetch()可以自动下移。 function fetch1($query) { $this->sql=$this->query($query); $this->r=mysql_fetch_array($this->sql); return $this->r; } //执行mysql_num_rows() function num($query)//此类的参数是$query就是sql语句 { $this->sql=$this->query($query); $this->num=mysql_num_rows($this->sql); return $this->num; } //执行numone(mysql_num_rows()) //此方法与num()的区别是:1、此方法的参数是$sql就是sql语句的执行结果。 function num1($sql) { $this->num=mysql_num_rows($sql); return $this->num; } //执行numone(mysql_num_rows()) //统计记录数 function gettotal($query) { $this->r=$this->fetch1($query); return $this->r['total']; } //执行free(mysql_result_free()) //此方法的参数是$sql就是sql语句的执行结果。只有在用到mysql_fetch_array的情况下用 function free($sql) { mysql_free_result($sql); } //执行seek(mysql_data_seek()) //此方法的参数是$sql就是sql语句的执行结果,$pit为执行指针的偏移数 function seek($sql,$pit) { mysql_data_seek($sql,$pit); } //执行id(mysql_insert_id()) function lastid()//取得最后一次执行mysql数据库id号 { $this->id=mysql_insert_id($GLOBALS['link']); if($this->id<0) { $this->id=$this->gettotal('SELECT last_insert_id() as total'); } return $this->id; } //返回影响数量(mysql_affected_rows()) function affectnum()//取得操作数据表后受影响的记录数 { return mysql_affected_rows($GLOBALS['link']); }}?>
$SQ="SELECT infotags,id FROM hd_ecms_shop_data_1 WHERE hd_ecms_shop_data_1.infotags LIKE '%我爱你%'";$result=mysqli_query($link,$SQ);$idStr='';if(mysqli_num_rows($result)){ $data=mysqli_fetch_all($result,MYSQL_ASSOC); foreach($data as $value){ if(isset($value['id'])) $idStr.=$value['id']; } $idStr=rtrim($idStr,','); echo $idStr;}
<?php$sqld=$empire->query('SELECT infotags,id FROM hd_ecms_shop_data_1 WHERE hd_ecms_shop_data_1.infotags LIKE "%现货促销产品%"');//查询副表下的infotags字段$idStr='';if($empire->num($sqld)){ $data=$empire->fetch($sqld); foreach($data as $key=>$value){ if(isset($value['id'])) $idStr.=$value['id']; } $idStr=rtrim($idStr,','); echo $idStr;}?>
$SQ="SELECT infotags,id FROM hd_ecms_shop_data_1 WHERE hd_ecms_shop_data_1.infotags LIKE '%我爱你%'";$result=mysqli_query($link,$SQ);$idStr='';if(mysqli_num_rows($result)){ $data=mysqli_fetch_all($result,MYSQL_ASSOC); foreach($data as $value){ if(isset($value['id'])) $idStr.=$value['id']; } $idStr=rtrim($idStr,','); echo $idStr;}
//代码误导你了 修改一下 $idStr.=$bqrd[id].',';}$idStr=rtrim($idStr,',')
另外 mysqli_fetch_all 是 PHP 的mysql数据库驱动 并没有给淘汰,只是服务器需要打开这个支持而已.有的服务器环境可能没有支持比如 sea...一般自己编译的服务器都可以轻松得到这个支持
另外 mysqli_fetch_all 是 PHP 的mysql数据库驱动 并没有给淘汰,只是服务器需要打开这个支持而已.有的服务器环境可能没有支持比如 sea...一般自己编译的服务器都可以轻松得到这个支持
$SQ="SELECT infotags,id FROM hd_ecms_shop_data_1 WHERE hd_ecms_shop_data_1.infotags LIKE '%我爱你%'";$result=mysqli_query($link,$SQ);$idStr='';if(mysqli_num_rows($result)){ $data=mysqli_fetch_all($result,MYSQL_ASSOC); foreach($data as $value){ if(isset($value['id'])) $idStr.=$value['id']; } $idStr=rtrim($idStr,','); echo $idStr;}
//hava a try SELECT GROUP_CONCAT(infotags SEPARATOR ",") as infotags,id FROM hd_ecms_shop_data_1 WHERE hd_ecms_shop_data_1.infotags LIKE '%我爱你%' GROUP BY infotags
可以查询出来id的数组再用implode来处理啊,为啥非要用sql查询来处理?
可以查询出来id的数组再用implode来处理啊,为啥非要用sql查询来处理?
//骚年,这是基本功的问题啊while($row=mysql_fetch_array($result)){ $id[]=$row['id'];}echo join(",",$id);
//骚年,这是基本功的问题啊while($row=mysql_fetch_array($result)){ $id[]=$row['id'];}echo join(",",$id);
//骚年,这是基本功的问题啊while($row=mysql_fetch_array($result)){ $id[]=$row['id'];}echo join(",",$id);
//骚年,这是基本功的问题啊while($row=mysql_fetch_array($result)){ $id[]=$row['id'];}echo join(",",$id);
//骚年,这是基本功的问题啊while($row=mysql_fetch_array($result)){ $id[]=$row['id'];}echo join(",",$id);