简体   繁体   中英

Laravel MySQL working on local computer but not remote

I'm trying to deploy my Laravel (4.1) App and I am having an issue with it working correctly with MySQL. I have narrowed it down to this line right here...

$curSessions = DB::table('sessions')->where('beginDate', '<', $curDate)->where('endDate', '>', $curDate)->get();

I tried changing this to the following and it returned as expected. (But too many records obviously)

$curSessions = DB::table('sessions')->where('beginDate', '<', $curDate)->get();

This does not work. It returns no errors and an empty array.

$curSessions = DB::table('sessions')->where('endDate', '>', $curDate)->get();

I then got it to echo the last statement it made ( DB::getQueryLog(); ) and the query that it outputs works when I input it in the SQL box on phpMyAdmin.

To be clear. The Query log shows:

array(1) { [0]=> array(3) { ["query"]=> string(64) "select * from sessions where beginDate < ? and endDate > ?" ["bindings"]=> array(2) { [0]=> string(8) "2014-1-3" [1]=> string(8) "2014-1-3" } ["time"]=> float(0.26) } }

And when I plug '2014-1-3' into the "?" it works.

The rub is... This works on my computer running MAMP, but not on the remote server. I checked the database structure to make sure that endDate and beginDate are both set as "Date," and they are.
Any idea?

您只需要确保包含前导0(如matpop建议)即可。

First you should check that your table date type is equal to what you are querying. For example if your table beginDate column is timestamp then your $curDate variable should be instance of timestamp. to do so you can do :

$curDate = Carbon::createFromDate("year", "month", "day");

Then you can use laravel built in eloquent like below :

$curSessions = Sessions::where('beginDate', '<', $curDate)
            ->where("endDate", ">", $curDate)->get();

FYI: there is another operator you can use for datetime period checking by using whereBetween , I'm putting an example here so you check if it does your job.

$curSessions = Sessions::whereBetween('beginDate', array($beginDate, $endDate))->first();

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