Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel never ending EXEC

I'm rewriting a program from pure PHP to Laravel and I have a problem with executing a stored procedure (I didn't write it).

When I try

$sheetLines = DB::select("exec XXXXXXX".dbo.PRICELIST '?'", [$id]); 

It keeps on going while PHP hasn't reached max memory. (increasing memory only makes it run longer)

Meanwhile, an old program it takes about 3 seconds and sends the response.

$tsql = "exec XXXXXXX.".dbo.PRICELIST '".$id."'";

Also when I'm calling other stored procedures from other modules everything works fine.

I have noticed that something like this also happens if I try for example:

DB::select(count(price) from orders group by price);
// would work with: count(price) as price_count

I have searched this problem a lot but have found no solution. I'll be thankful for any help

like image 619
NoOorZ24 Avatar asked Mar 27 '18 12:03

NoOorZ24


People also ask

Does Laravel have exception handling?

Laravel comes with Exception handling, however this can impact the clarity of your tests, let's fix just that. Exception handling.

How to start a queue worker in Laravel?

To start Laravel’s queue worker, you need to run php artisan queue:work. Production environments require you to have the workers running all the time. queue:work command itself can fail due to many reasons, such as exceeding the maximum timeout. Checking the server manually to make sure if the queue worker is up is not an option.

How to fix Laravel 404 page not found?

Another quick fix we can make is using User::findOrFail () instead of just find () – then if user is not found, Laravel would show 404 page with text “Sorry, the page you are looking for could not be found.”. But this is a default 404 page for the whole project, so not massively helpful to user, is it?

How do I seed a database in Laravel?

Laravel includes the ability to seed your database with data using seed classes. All seed classes are stored in the database/seeders directory. By default, a DatabaseSeeder class is defined for you. From this class, you may use the call method to run other seed classes, allowing you to control the seeding order.


2 Answers

I would access the underlying PDO driver to execute the stored procedure.

Try

$db = DB::getPdo();

$stmt = $db->prepare("EXEC XXXXXXX.dbo.PRICELIST :id");
$stmt->bindValue(':id', $id);

$result = $stmt->execute();

If that fails to work you could try the query method;

$query = DB::getPdo()->query("EXEC XXXXXXX.dbo.PRICELIST $id");

Regarding your count issue, in Laravel's Eloquent you can do the following;

$count = DB::table('orders')->groupBy('price')->count('price');
like image 118
Simon R Avatar answered Sep 25 '22 14:09

Simon R


I've little experience with PHP, but seen:

set nocount on

Cause oddball behavior in some MSSQL drivers. Especially a stored procedure returns a single recordset, but doesn't output a "(x row(s) affected)" type messages. (Messages can be seen when manually executing a query in SQL Server Management Studio.) My rule of thumb for complex stored procedure is to add...

set nocount on

...at the start of a stored procedure and conclude with...

set nocount off

....just before the final output.

Example:

create proc spTester 
as 
begin
set nocount off

-- {... do lots of crazy processing ...}

-- ok, ready to return the final output
set nocount off
select 1 as colA, 2 as colB

end
like image 41
James Moody Avatar answered Sep 23 '22 14:09

James Moody