SQL Query在PHP中返回NULL,但在PHPMyAdmin中工作正常

时间:2016-10-10 11:26:02

标签: php mysql phpmyadmin

运行此代码:

public function findBooking($start_date, $pitches, $nights, $people, $first_name, $last_name, $email){

    $error = false;
    $bookings = array();

    $this->db_connection = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
    if (!$this->db_connection->connect_errno) {
                   if((!$start_date == '') && (!$nights == '')){
            $end_date = date('Y-m-d', strtotime($start_date. ' + ' . ($nights-1) . ' days'));
        } else {
            $end_date = '';
        }

        $find_booking_sql = "SELECT * FROM booking LEFT JOIN camper ON booking.camper_id = camper.camper_id
                             WHERE (start_date LIKE  '%$start_date%')
                             AND (end_date LIKE  '%$end_date%') 
                             AND (pitches LIKE  '%$pitches%') 
                             AND (people LIKE  '%$people%')
                             AND (first_name LIKE  '%$first_name%')
                             AND (last_name LIKE  '%$last_name%')
                             AND (email LIKE  '%$email%')
                             ORDER BY start_date ASC";
        $find_booking_result = $this->db_connection->query($find_booking_sql);
        if ((!$find_booking_result->num_rows == 0)) {  
            // Not run
            }
        } else {
            $error = 'No bookings found';
        }

    }

    return array(
        'bookings' => $bookings,
        'error' => $this->db_connection->error,
        'debug' => $find_booking_sql,
        'result' => $find_booking_result
    );
}

在PHP中运行时返回current_field":null,"field_count":null,"lengths":null,"num_rows":null,"type":null,但如果我从PHPMyAdmin运行SQL,它可以正常工作。

我已经检查了类似的问题,但解决方案无法解决这个问题 - 我尝试删除了这些问题,并且只运行了一个查询。

示例生成的SQL:

SELECT *
FROM booking
LEFT JOIN camper ON booking.camper_id = camper.camper_id
WHERE (start_date LIKE '%')
    AND (end_date LIKE '%')
    AND (pitches LIKE '%')
    AND (people LIKE '%')
    AND (first_name LIKE '%')
    AND (last_name LIKE '%') AND (email LIKE '%')
ORDER BY start_date ASC`

1 个答案:

答案 0 :(得分:0)

我的错误 - 我在实时空数据库上运行PHP查询,但在登台数据库上运行PHPMyAdmin查询。

我切换了数据库,原始代码运行正常。