mysql查询在phpmyadmin中工作,但在php代码中不工作为什么?

gjmwrych  于 2021-06-17  发布在  Mysql
关注(0)|答案(1)|浏览(256)

我正在使用以下查询,并希望查询结果显示在单引号内,如“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行的appointment\u time in/applications/mamp/htdocs/booking system/index.php
注意:第140行的/applications/mamp/htdocs/booking system/index.php中未定义索引:appointment\u end\u time,
为什么会这样?

s3fp2yjn

s3fp2yjn1#

您需要在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'];
}

相关问题