Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/61.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函数使用join选择_Php_Mysql - Fatal编程技术网

使用标准php函数使用join选择

使用标准php函数使用join选择,php,mysql,Php,Mysql,我尝试使用php函数从数据库中选择数据,我使用“标准”php函数: function select($table, $where, $other=''){ try{ $a = array(); $w = ""; $other=" ".$other; foreach ($where as $key => $value) { $w .= " and

我尝试使用php函数从数据库中选择数据,我使用“标准”php函数:

 function select($table, $where, $other=''){
        try{
            $a = array();
            $w = "";
            $other=" ".$other;
            foreach ($where as $key => $value) {
                $w .= " and " .$key. " like :".$key;
                $a[":".$key] = $value;
            }
            $stmt = $this->bd->prepare("select * from ".$table." where 1=1 ". $w.$other); 
            $stmt->execute($a);
            $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
            if(count($rows)<=0){
                $reponse["statut"] = "warning";
                $reponse["message"] = "no data found.";
            }else{
                $reponse["statut"] = "Success";
                $reponse["message"] = "Success";
            }
                $reponse["data"] = $rows;

        }catch(Exception $e){
            $reponse["statut"] = "Error";
            $reponse["message"] = 'selection failed: ' .$e->getMessage();
            $reponse["data"] = null;
        }
        return $reponse;
    }
函数选择($table,$where,$other=''){
试一试{
$a=数组();
$w=“”;
$other=“”.$other;
foreach($key=>$value){
$w.=”和“$key.”如:“..$key;
$a[“:”$key]=$value;
}
$stmt=$this->bd->prepare(“从“$table.”中选择*,其中1=1“$w.$other”);
$stmt->execute($a);
$rows=$stmt->fetchAll(PDO::FETCH_ASSOC);
if(count($rows)getMessage();
$reponse[“数据”]=null;
}
返回$response;
}
该函数的工作令人满意。只有当我打算使用联接并从两个表中进行选择时,我才会遇到问题。 假设我们有两张桌子:


  • id猫|猫名|


  • 1 | aaaaa |

  • 2 | aaadda |
  • 3 |萨阿卡|
  • 4 |啊哈|



  • id类别|子类别|
  • 1 | 2|
  • 1 | 3|
  • 1 | 4|
  • 2 | 3|
  • 2 | 4|

如何使用MY函数选择具有给定id cat的a类别的子名称???

您只需使用两个表中存在的列向第二个表进行简单的左联接即可

$stmt = $this->bd->prepare("SELECT * 
                            FROM $table
                            LEFT JOIN $table2 USING id-cat
                            WHERE 1=1 ". $w.$other); 

是的,这会起作用,但我正在寻找一种方法来自定义我的函数,这样它就可以处理许多联接表和许多联接字段!!我考虑更改函数,因为没有提供答案。。