我添加了一条if语句,如果查询没有返回空,它将显示数据$playersList = DB::table('pickup_results')
->select(DB::raw("playerID"),
DB::raw("COUNT(CASE WHEN gameResult = 'Win' THEN 1 END) AS wins"),
DB::raw("COUNT(CASE WHEN gameResult = 'Loss' THEN 1 END) AS loss"),
DB::raw("COUNT(CASE WHEN gameResult = 'Tie' THEN 1 END) AS tie")
)
->where(DB::raw("from_unixtime(checkSumID) > date_sub(now(), interval 30 day)"))
->groupBy('playerID')
->orderBy('wins','DESC')
->get();
但是,如果查询为空,则应该为
if (!empty($query)
代码:
elseif (empty($query){
echo 'There are no cars impounded'
}
没有if语句,它可以正常工作,但是在表中没有任何结果,它将给出错误并中断页面的其他功能
<?php
try {
$con= new PDO('mysql:host=sql14.jnb1.host-h.net;dbname=DB_Name', "silkgeqafj_4", "Password");
$con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$query =
"SELECT vehicle_plate as 'Plate',
vehicle_color as 'Color',
vehicle_model as 'Model',
vehicle_ownername as 'Owner Name',
vehicle_impoundedTill as 'Release Date'
FROM vehicles where vehicle_isImpounded = true ";
if (!empty($query){
//first pass just gets the column names
print "<table> ";
$result = $con->query($query);
//return only the first row (we only need field names)
$row = $result->fetch(PDO::FETCH_ASSOC);
print " <tr> ";
foreach ($row as $field => $value){
print " <th>$field</th> ";
} // end foreach
print " </tr> ";
//second query gets the data
$data = $con->query($query);
$data->setFetchMode(PDO::FETCH_ASSOC);
foreach($data as $row){
print " <tr> ";
foreach ($row as $name=>$value){
print " <td>$value</td> ";
} // end field loop
print " </tr> ";
} // end record loop
print "</table> ";
elseif (empty($query){
echo 'There are no cars impounded'
}
}
}
catch(PDOException $e) {
echo 'ERROR: ' . $e->getMessage();
} // end try
?>