我从外面传入$ids,$ids是如 '2676','2675','2674','2683','2682'此类id号,但每次都显示error,但我用输出来的sql语句在SQLyog里查却可以查出来,纠结啊!!!请各位帮我看看,小弟实在无力啦!!!
页面上输出:
select `p_name`, `p_53kf_price` from `cs_product` where `id` in ('2676','2675','2674','2683','2682')error
功能代码如下:function get_product($ids)
{
$sql="select `p_name`, `p_53kf_price` from `cs_product` where `id` in (".$ids.")";
echo $sql;
$re1=db_query($sql);
//echo "<br/>".count($re1)."<br>".$re1."<br/>";
if($re1!==false && count($re1)>0)
{
foreach($re1 as $v)
{
}
}else{
echo "error";
}
return $list;}数据库代码如下/*返回多条记录 2维数组*/
function db_query($sql)
{
global $dblink; $rs = mysqli_query($dblink,$sql);
if (!$rs)
{
error_log("SQL ERROR: $sql",0);
return false;
} if ($rs===true)
{
//可能执行insert update delete
$rows = true;
}
else
{
$rows = array();
while ($row=mysqli_fetch_assoc($rs)) $rows[] = $row;
mysqli_free_result($rs);
}
return $rows;
}
页面上输出:
select `p_name`, `p_53kf_price` from `cs_product` where `id` in ('2676','2675','2674','2683','2682')error
功能代码如下:function get_product($ids)
{
$sql="select `p_name`, `p_53kf_price` from `cs_product` where `id` in (".$ids.")";
echo $sql;
$re1=db_query($sql);
//echo "<br/>".count($re1)."<br>".$re1."<br/>";
if($re1!==false && count($re1)>0)
{
foreach($re1 as $v)
{
}
}else{
echo "error";
}
return $list;}数据库代码如下/*返回多条记录 2维数组*/
function db_query($sql)
{
global $dblink; $rs = mysqli_query($dblink,$sql);
if (!$rs)
{
error_log("SQL ERROR: $sql",0);
return false;
} if ($rs===true)
{
//可能执行insert update delete
$rows = true;
}
else
{
$rows = array();
while ($row=mysqli_fetch_assoc($rs)) $rows[] = $row;
mysqli_free_result($rs);
}
return $rows;
}
select p_name, p_53kf_price from cs_product where id in (".$ids.")
print_r($rel);
{
//可能执行insert update delete
$rows = true;
}
else
{
$rows = array();
while ($row=mysqli_fetch_assoc($rs)) $rows[] = $row;
mysqli_free_result($rs);
}
return $rows;
这里有问题 ,你的变量$rows作用域有问题 ,if 和else结束后$rows就没了所以没有结果
解决办法是在if之前加上$rows = array()
改成 $rs = mysql_query($dblink,$sql);楼主试试!
$ids=str_replace("'","'",$ids);
先替换掉‘,谁能解释下?
这里是不是应该这样(’".$ids."‘)";
另外,你传进来的$ids到底是几个id呢,是不是需要循环一下读出每一个id啊
$sql="select `p_name`, `p_53kf_price` from `cs_product` where `id` in ("'".implode("','", $ids)."')";
$query = mysql_query($sql);
while($value = mysql_fetch_array($query)) {
$list[] = $value;
}
你的ID是数值