1

I have come across a situation where the columns generated by my stored procedure are dynamic. I want to know, do we have any method to get the columns names of a result set when the result set returned by stored procedure is empty in Laravel?

I can easily access columns by iterations over the result set when it is not empty but is there any method to access the column names when the data is empty?

That's what I mean to be exact.

 $data = Schema::getColumnListing("call conflictReport(123,'2016-08-01 09:00:31','2016-08-01 09:00:31')");

My procedure takes 3 parameters as in parameters.

When the result set is empty, I can't access the column names. I am using getColumnListing of Laravel.

Danyal Sandeelo
  • 12,196
  • 10
  • 47
  • 78

1 Answers1

1

Here's how to do it directly with PDO. With Laravel, you should be able to us the getPdo() function of your MySQLConnection object.

<?php

$pdo = new PDO('mysql:host=localhost;dbname=test', 'root', 'xxxxxxxx');

$stmt = $pdo->query('select * from mytable where false');
$colCount = $stmt->columnCount();
for ($col = 0; $col < $colCount; ++$col) {
    print_r($stmt->getColumnMeta($col));
}

I tested and found this works with a CALL query too:

$stmt = $pdo->query('call myproc()');

Basically any statement with a result set has column metadata.

See PDOStatement::getColumnMeta() for more information.

Bill Karwin
  • 538,548
  • 86
  • 673
  • 828
  • updated the question basically I am accessing stored procedure using laravel – Danyal Sandeelo Aug 08 '16 at 07:40
  • we use this --> $data = Schema::getColumnListing("call conflictReport(123,'2016-08-01 09:00:31','2016-08-01 09:00:31')"); in laravel to call procedure..did not work though – Danyal Sandeelo Aug 08 '16 at 07:46
  • I'm not a Laravel user, but it took me about 5 seconds to find the docs for [`getColumnListing()`](https://laravel.com/api/5.1/Illuminate/Database/Schema/Builder.html#method_getColumnListing) and see that it takes a table name as an argument, not a SQL statement. I think you should read the docs more carefully. – Bill Karwin Aug 08 '16 at 07:49
  • He just said that he needs returning columns names from stored procedure, not a table. I deleted my answer. – revo Aug 08 '16 at 08:20