我正在使用以下查询,希望查询结果显示在单引号内,例如“ 10AM”。
$sql2 = mysqli_query($con, "SELECT concat('''', appointment_time ,''''), concat('''', appointment_end_time ,'''') FROM Appointments WHERE appointment_with LIKE 'Sarah'");
if (!$sql2) {
die('Invalid query: ' . mysqli_error());
}
while ($row2 = mysqli_fetch_assoc($sql2)) {
$time = array($row2['appointment_time']. "," ." " . $row2['appointment_end_time']);
$appointment = [];
foreach ($time as $appointment){
echo $appointment;
}
当我在phpmyadmin中运行代码但在我的php代码中运行代码时,此方法工作正常,因为我收到以下消息。
注意:未定义的索引:第140行/Applications/MAMP/htdocs/booking-system/index.php中的约会时间
通知:未定义的索引:/Applications/MAMP/htdocs/booking-system/index.php在第140行中的约会结束时间 ,
为什么会这样?
答案 0 :(得分:2)
您需要在SQL中为字段指定别名...
$sql2 = mysqli_query($con, "SELECT concat('''', appointment_time ,'''') as appointment_time,
concat('''', appointment_end_time ,'''') as appointment_end_time
FROM Appointments
WHERE appointment_with LIKE 'Sarah'");
对于主循环,您似乎要构建一个数组以再次打印出来,可以将代码减少为...
while ($row2 = mysqli_fetch_assoc($sql2)) {
echo $row2['appointment_time']. ", " .
$row2['appointment_end_time'];
}