Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
php函数获取两个可选参数并编写where类_Php_Mysql - Fatal编程技术网

php函数获取两个可选参数并编写where类

php函数获取两个可选参数并编写where类,php,mysql,Php,Mysql,我有一个函数,它计算表中记录的总数,并得到两个参数,这两个参数都是可选的 function getTotal($id=0,$id1=0) { ($id==0?$addQuery="":$addQuery=" where art_id=".$id); if($id1<>0 && $id==0) { $addQuery=" where up_type=".$id1 }

我有一个函数,它计算表中记录的总数,并得到两个参数,这两个参数都是可选的

function getTotal($id=0,$id1=0)
    {
        ($id==0?$addQuery="":$addQuery=" where art_id=".$id);
        if($id1<>0 && $id==0)
        {
        $addQuery=" where up_type=".$id1
        }
        if($id1<>0 && $id<>0)
        {
        $addQuery=" and up_type=".$id1
        }
        mysql_set_charset('utf8');
        $query="SELECT COUNT(id) FROM tbl_up ".$addQuery;
        $result=$this->query($query,1);
        return $result;
    }
第二份土地

谢谢

函数getTotal($id=0,$id1=0)
{
$addQuery=“其中1=1”
如果($ID0)$addQuery.=”和art_id=“.$id
如果($id10)$addQuery.=”和up_type=“.$id1
mysql_集_字符集('utf8');
$query=“从tbl\U up中选择计数(id)。$addQuery;
$result=$this->query($query,1);
返回$result;
}
这是一种测试多个案例的通用方法

函数getTotal($id=0,$id1=0)
{
$addQuery=“其中1=1”
如果($ID0)$addQuery.=”和art_id=“.$id
如果($id10)$addQuery.=”和up_type=“.$id1
mysql_集_字符集('utf8');
$query=“从tbl\U up中选择计数(id)。$addQuery;
$result=$this->query($query,1);
返回$result;
}
这是一种测试多个案例的通用方法

尝试:

function getTotal($id = 0,$id1 = 0) {
  // sorry, I rewrite the first expression to this, easier to read IMHO
  $addQuery = $id == 0 ? "" : " where art_id='".mysql_real_escape_string($id)."'";
  if ($id1 <> 0)
    $addQuery .= ($id == 0 ? " where" : " and") . " up_type='".mysql_real_escape_string($id1)."'";
  mysql_set_charset('utf8');
  $query="SELECT COUNT(id) FROM tbl_up ".$addQuery;
  $result=$this->query($query,1);
  return $result;
}
函数getTotal($id=0,$id1=0){ //对不起,我把第一个表达式改写成这个,更容易理解 $addQuery=$id==0?”:“其中art\u id='”。mysql\u real\u escape\u string($id)。“; 如果($ID10) $addQuery.=($id==0?“:”和“)”up\u type='”。mysql\u real\u escape\u string($id1)。“; mysql_集_字符集('utf8'); $query=“从tbl\U up中选择计数(id)。$addQuery; $result=$this->query($query,1); 返回$result; } 试试:

函数getTotal($id=0,$id1=0){ //对不起,我把第一个表达式改写成这个,更容易理解 $addQuery=$id==0?”:“其中art\u id='”。mysql\u real\u escape\u string($id)。“; 如果($ID10) $addQuery.=($id==0?“:”和“)”up\u type='”。mysql\u real\u escape\u string($id1)。“; mysql_集_字符集('utf8'); $query=“从tbl\U up中选择计数(id)。$addQuery; $result=$this->query($query,1); 返回$result; }
您缺少
WHERE
第1条,如果您忘记(单)引用包含空格的注入的
$id
的id将不起作用,那么在此截取之前采取的反SQL注入措施也将失败。如果您忘记(单)引用,您将缺少
WHERE
第1条引用包含空格的注入的
$id
的id将不起作用,并且在此截取之前采取的反SQL注入措施也将失败。这看起来像SQL注入孔,请参阅:这看起来像SQL注入孔,请参阅:-1,如果忘记(单次)引用包含空格的注入的$id的id将不起作用,此外,在此之前采取的反SQL注入措施也将失败约翰在0秒后编辑请不要因为OP没有要求的事情而否决投票。我回答了他想做的事,句号。注入应该是另一个话题。我投了反对票,因为在生产中使用此代码是危险的。如果你把帖子编辑得不危险,我会非常乐意投票。-1,如果你忘了(单)引用包含空格的注入$id的id,那么你在截取之前采取的反SQL注入措施也将失败约翰在0秒后编辑请不要因为OP没有要求的事情而否决投票。我回答了他想做的事,句号。注入应该是另一个话题。我投了反对票,因为在生产中使用此代码是危险的。如果你把这篇文章编辑得不危险,我会非常乐意投你一票。
function getTotal($id=0,$id1=0)
{
    $addQuery="where 1=1"
    if($id <>0)  $addQuery.=" and art_id =".$id
    if($id1<>0)  $addQuery.=" and up_type=".$id1

    mysql_set_charset('utf8');
    $query="SELECT COUNT(id) FROM tbl_up ".$addQuery;
    $result=$this->query($query,1);
    return $result;
}
function getTotal($id = 0,$id1 = 0) {
  // sorry, I rewrite the first expression to this, easier to read IMHO
  $addQuery = $id == 0 ? "" : " where art_id='".mysql_real_escape_string($id)."'";
  if ($id1 <> 0)
    $addQuery .= ($id == 0 ? " where" : " and") . " up_type='".mysql_real_escape_string($id1)."'";
  mysql_set_charset('utf8');
  $query="SELECT COUNT(id) FROM tbl_up ".$addQuery;
  $result=$this->query($query,1);
  return $result;
}
function getTotal($id=0,$id1=0)
    {
        $where = array();
        if ($id) $where[]='`art_id`="'.$id.'"';
        if ($id1) $where[] = '`up_type`="'.$id1.'"';
        $where = (!count($where) ? '' : 'WHERE '.implode(' AND ', $where));
            $query="SELECT COUNT(id) FROM tbl_up ".$where;
            mysql_set_charset('utf8');
            $result=$this->query($query,1);
            return $result;
    }