sql出来的集合如何变换成想要的字符串 100分!

WBOY
Release: 2016-06-23 13:39:26
Original
908 people have browsed it

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;}
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login

$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;}
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login


写了一下还是不行,但我感觉你这个思路是正确的,只是我不会利我其中的一个数据库操作类
比如你在其中用的一个函数是 mysqli_fetch_all
我查了一下资料好象这个函数被取缔了.
另外我的系统中有一个数据库函数操作类,帖上来
<?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']);	}}?>
Copy after login

能不能改用这里的方法呢,比如假如
先实例化这个类
$empire=news mysqlquery();
然后用$empire->的方法怎么改写上面的呢,

$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;}
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login


<?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;}?>
Copy after login

我改了一下,但是没有成功,不知哪错了

$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;}
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login


我这么写,出来结果了,但是全是乱七八糟的一些数字连在一起了
$sqld=$empire->query('SELECT infotags,id FROM hd_ecms_shop_data_1 WHERE hd_ecms_shop_data_1.infotags LIKE "%我爱你%"');//查询副表下的infotags字段
$idjh=array();
while($bqrd=$empire->fetch($sqld)){
if($bqrd[id])
$idStr.=$bqrd[id];
//$idStr=rtrim($idStr,',');
echo $idStr;

//代码误导你了 修改一下  $idStr.=$bqrd[id].',';}$idStr=rtrim($idStr,',')
Copy after login

另外 mysqli_fetch_all 是 PHP 的mysql数据库驱动 并没有给淘汰,只是服务器需要打开这个支持而已.有的服务器环境可能没有支持比如 sea...一般自己编译的服务器都可以轻松得到这个支持

另外 mysqli_fetch_all 是 PHP 的mysql数据库驱动 并没有给淘汰,只是服务器需要打开这个支持而已.有的服务器环境可能没有支持比如 sea...一般自己编译的服务器都可以轻松得到这个支持



这段代码改成while不用foreach的形式可以吗,并且如何改呢
$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;}
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login



你这个能改成while形式吗,改完后就可以加分了

//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
Copy after login

可以查询出来id的数组再用implode来处理啊,为啥非要用sql查询来处理?

可以查询出来id的数组再用implode来处理啊,为啥非要用sql查询来处理?


怎么写,无码无真相

//骚年,这是基本功的问题啊while($row=mysql_fetch_array($result)){    $id[]=$row['id'];}echo join(",",$id);
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login

//骚年,这是基本功的问题啊while($row=mysql_fetch_array($result)){    $id[]=$row['id'];}echo join(",",$id);
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login


上仙,再请教一下
为啥
echo $row[id];//打印不出值,也不报错
//echo join(",",$id);//能打印出值来

//骚年,这是基本功的问题啊while($row=mysql_fetch_array($result)){    $id[]=$row['id'];}echo join(",",$id);
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login


上仙我搞错了,思维完全混乱造成的,
Array ( [0] => 9 [1] => 38 [2] => 39 [3] => 40 [4] => 36 [5] => 37 )

//骚年,这是基本功的问题啊while($row=mysql_fetch_array($result)){    $id[]=$row['id'];}echo join(",",$id);
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login


上仙的方法不错,加分,希望今后再接再历,不要辜负我们对你的期望!钦此!

//骚年,这是基本功的问题啊while($row=mysql_fetch_array($result)){    $id[]=$row['id'];}echo join(",",$id);
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login


嗯,上仙的方法不错,加分了,上仙!
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!