如果原因,Laravel查询

时间:2018-04-03 17:29:06

标签: laravel laravel-5

我运行以下查询,如果因为原因那么原因应用如下,但当我运行下面的查询它给我“内存不足(分配503316480)(试图分配492834816字节)”错误,所以另一个容易如果条件,检查上述原因的方法。

以下是我的代码。

$user_id = "";
$doctor_id = "";
$start_date = "";
$end_date = "";

if(isset($request->user_id)) {
    $user_id = $request->user_id;
}
if(isset($request->doctor_id)) {
    $doctor_id = $request->doctor_id;   
}
if(isset($request->start_date)) {
    $start_date = $request->start_date; 
}
if(isset($request->end_date)) {
    $end_date = $request->end_date; 
}

$query = DB::table('request_approves')
            ->join('city_masters', 'request_approves.city_id', '=', 'city_masters.id')
            ->join('doctors', 'request_approves.doctor_id', '=', 'doctors.id')
            ->select('doctors.*','city_masters.*','request_approves.*');

            if($user_id !== "") {
                $query->where('request_approves.user_id', 2);                        
            }

            if($doctor_id !== "") {
                $query->where('request_approves.doctor_id',3);    
            }

            if($start_date !== "" & $end_date !==""){
                $query->whereBetween('request_approves.approve_time', array($start_date,$end_date));    
            }
            $query->get();

2 个答案:

答案 0 :(得分:2)

PHP也使用内存来执行查询。因此,从PHP应用程序执行查询确实会使用比直接从MySQL执行它们更多的内存。

我做了一个简单的计算,503316480字节输出到503MB。所以我建议修改你的php.ini设置。这应该让你输出数据,然后你可以调整你的SQL。

在你的php.ini

<?php
error_reporting(~E_NOTICE);
set_time_limit (0);

$address = "192.168.1.100"; //ip here
$port = 8996; //port number  here
$max_clients = 10;

if(!($sock = socket_create(AF_INET, SOCK_STREAM, 0)))
{
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);

die("Couldn't create socket: [$errorcode] $errormsg \n");
}

echo "Socket created \n";

// Bind the source address
if( !socket_bind($sock, $address , 5000) )
{
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);

die("Could not bind socket : [$errorcode] $errormsg \n");
}

echo "Socket bind OK \n";

if(!socket_listen ($sock , 10))
{
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);

die("Could not listen on socket : [$errorcode] $errormsg \n");
}

echo "Socket listen OK \n";

echo "Waiting for incoming connections... \n";

//array of client sockets
$client_socks = array();

//array of sockets to read
$read = array();

//start loop to listen for incoming connections and process existing 
connections
while (true) 
{
//prepare array of readable client sockets
$read = array();

//first socket is the master socket
$read[0] = $sock;

//now add the existing client sockets
for ($i = 0; $i < $max_clients; $i++)
{
    if($client_socks[$i] != null)
    {
        $read[$i+1] = $client_socks[$i];
    }
}

//now call select - blocking call
if(socket_select($read , $write , $except , null) === false)
{
    $errorcode = socket_last_error();
    $errormsg = socket_strerror($errorcode);

    die("Could not listen on socket : [$errorcode] $errormsg \n");
}

//if ready contains the master socket, then a new connection has come in
if (in_array($sock, $read)) 
{
    for ($i = 0; $i < $max_clients; $i++)
    {
        if ($client_socks[$i] == null) 
        {
            $client_socks[$i] = socket_accept($sock);

            //display information about the client who is connected
            if(socket_getpeername($client_socks[$i], $address, $port))
            {
                echo "Client $address : $port is now connected to us. \n";
            }

            //Send Welcome message to client
            $message = "Welcome to php socket server version 1.0 \n";
            $message .= "Enter a message and press enter, and i shall reply 
back \n";


            socket_write($client_socks[$i] , $message);
            break;
        }
    }
  }

//check each client if they send any data
for ($i = 0; $i < $max_clients; $i++)
{
    if (in_array($client_socks[$i] , $read))
    {
        $input = socket_read($client_socks[$i] , 1024);

        if ($input == null) 
        {
            //zero length string meaning disconnected, remove and close the 
socket
            unset($client_socks[$i]);
            socket_close($client_socks[$i]);
        }

        $n = trim($input);

        $output = "OK ... $input";

        echo "Sending output to client \n";

        //send response to client
        socket_write($client_socks[$i] , $output);
    }
   }
}
?>

答案 1 :(得分:0)

不确定它发生了什么,但你的代码对我来说似乎不对。 在if语句中,您执行类似$query->where('request_approves.user_id', 2);之类的操作,但此返回值的位置是什么?要继续相同的查询,您应该这样做:

$query = DB::table('request_approves')
        ->join('city_masters', 'request_approves.city_id', '=', 'city_masters.id')
        ->join('doctors', 'request_approves.doctor_id', '=', 'doctors.id')
        ->select('doctors.*','city_masters.*','request_approves.*');

        if($user_id !== "") {
            $query = $query->where('request_approves.user_id', 2);                        
        }

        if($doctor_id !== "") {
            $query = $query->where('request_approves.doctor_id',3);    
        }

        if($start_date !== "" & $end_date !==""){
            $query = $query->whereBetween('request_approves.approve_time', array($start_date,$end_date));    
        }
        $query = $query->get();