Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/266.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中简单数据库查询代码的问题_Php_Mysql - Fatal编程技术网

PhP中简单数据库查询代码的问题

PhP中简单数据库查询代码的问题,php,mysql,Php,Mysql,我是PhP新手。我从其他团队成员那里得到了一个代码,只是想做一个小的改变 代码是从DB获取项目列表。它使用“where”子句将行业参数与sql查询中的行业输入参数进行比较。这段代码运行良好。我只想将查询->where子句的“=”替换为“LIKE”比较 下面是当前代码中的“=”代码 public function feed($industry=null){ $query = DB::table('startups as s') ->leftjoin('founders as

我是PhP新手。我从其他团队成员那里得到了一个代码,只是想做一个小的改变

代码是从DB获取项目列表。它使用“where”子句将行业参数与sql查询中的行业输入参数进行比较。这段代码运行良好。我只想将查询->where子句的“=”替换为“LIKE”比较

下面是当前代码中的“=”代码

 public function feed($industry=null){
    $query = DB::table('startups as s')
    ->leftjoin('founders as fd','fd.startup_id','=','s.id');
    if($industry)
        $query->where('s.industry','=',$industry);      
    $result = $query->select('s.id as sid','fd.name as fdname','fd.email','fd.contact','s.*','s.created_at',DB::raw("(select sum(f.fund_amount) from funds as f where f.startup_id=s.id and f.fund_amount>0)as 'total'"))
    ->groupBy('s.id')
    ->get();
    return json_encode($result);        
},
下面是我多次尝试用LIKE替换“=”的一个例子(但到目前为止,没有一个选项起作用——看起来很简单,但在PhP中仍然不起作用,所以我遗漏了一些东西)


看起来您已经正确地修改了where子句,但请尝试以下操作:

public function feed($industry=null){

    $query = DB::table('startups as s')
        ->leftjoin('founders as fd','fd.startup_id','=','s.id');
    if( $industry ){
        $query->where('s.industry','LIKE', '%'.$industry.'%');     
    }

    $result = $query->select('s.id as sid','fd.name as fdname','fd.email','fd.contact','s.*','s.created_at',DB::raw("(select sum(f.fund_amount) from funds as f where f.startup_id=s.id and f.fund_amount > 0)as 'total'"))
        ->groupBy('s.id')
        ->get();

    return json_encode($result);        
}

希望这能起作用并有所帮助

看起来您已经正确地修改了where子句,但请尝试以下操作:

public function feed($industry=null){

    $query = DB::table('startups as s')
        ->leftjoin('founders as fd','fd.startup_id','=','s.id');
    if( $industry ){
        $query->where('s.industry','LIKE', '%'.$industry.'%');     
    }

    $result = $query->select('s.id as sid','fd.name as fdname','fd.email','fd.contact','s.*','s.created_at',DB::raw("(select sum(f.fund_amount) from funds as f where f.startup_id=s.id and f.fund_amount > 0)as 'total'"))
        ->groupBy('s.id')
        ->get();

    return json_encode($result);        
}

希望这能起作用,并有助于查看
DB
类及其文档以寻找线索。它似乎不是PHP本身的一部分。如果这是一个免费的图书馆,我建议你把它的名字和版本贴在这里。就像随机猜测一样,试着把空格放在
的两边,比如“
”。添加空格没有任何帮助。@Cobra\u Fast-不确定,但我认为它是laravel框架,正如我看到的文档一样-你在使用什么框架?查看
DB
类及其文档以获取线索。它似乎不是PHP本身的一部分。如果这是一个免费的图书馆,我建议你把它的名字和版本贴在这里。就像随机猜测一样,尝试将空格放在
'的任一侧,如“
”。添加空格没有任何帮助。@Cobra\u Fast-不确定,但我认为它是laravel框架,正如我看到的文档一样-您使用的是什么框架?之前尝试过这个,然后再试一次,但没有修复它…不过感谢您的建议。如果您按照线程记录正在执行的db查询,结果是什么实际上正在运行?之前尝试过,然后再次尝试,但没有修复…不过感谢您的建议。如果您按照线程记录正在执行的db查询,那么实际运行的是什么?