简体   繁体   中英

Getting data from mysql between two dates for a joomla project

I must modify a simple project to display some values in a different way.

I got a MySQL database, in which are stored reservations.

I want to get all data from a reservation, selecting the room and the period.

that's my query:

function getReservation($idroom,$date){
    $db=JFactory::getDbo();
    $query=$db->getQuery(true);
    $datecheck=$date->format('%Y-%m-%d');

    $query
         ->select($db->quoteName(array('idroom','status','idguests','nguests','value_paid','value_pending','value_full','valid_from','valid_to','today','extra_ids','nchilds')))
         ->from($db->quoteName('#__bookitbooking'))
         ->where($db->quoteName('idroom')."=".$idroom)
         ->and ( $db->quoteValue($datecheck) . "BETWEEN" . $db->quoteName('valid_from') . "AND" . $db->quoteName('valid_to'));
    $db->setQuery($query);

    $res = $db->loadObjectList();
    return $res;
}

But the and clause seems to not have effect, in fact when I get the count of reservation stored from the query, I got all that matches with the $idroom value.

With that function I want to know if for a particular date exists a reservation, and if it exists I want to get all the parameters.

Where's my mistake?

Obviously I can obtain at maximum just one reservation object.

There is an error with your syntax. The "and" is assumed in the second where clause, where you check the dates, when using the core Joomla DBO class. Change the second where clause line to:

->where(INSERT DATE QUERY HERE);

* EDIT *

Another couple of things I noticed are the date formatting and your method for wrapping values in quotes for the table name and the $datecheck variable. Not to say the date formatting you're using shouldn't or won't work, but traditionally I've always formatted my dates using MySQL syntax. Try this, which accounts for the MySQL date formatting and using the DBO method intended for wrapping table names:

function getReservation($idroom,$date){
    $db=JFactory::getDbo();
    $query=$db->getQuery(true);
    $datecheck= date('Y-m-d H:i:s');

    $query
         ->select($db-quoteName(array('idroom','status','idguests','nguests','value_paid','value_pending','value_full','valid_from','valid_to','today','extra_ids','nchilds')))
         ->from($db->quote('#__bookitbooking'))
         ->where($db->quoteName('idroom')."=".$idroom)
         ->where( $db->quoteName($datecheck) . "BETWEEN" . $db->quoteName('valid_from') . "AND" . $db->quoteName('valid_to'));

    $db->setQuery($query);

    $res = $db->loadObjectList();
    return $res;

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM