Vasu
Vasu

Reputation: 69

Call a mysql Stored Function from PHP 5.3 application

I'm having issues calling a stored function in MYSQL DB from a PHP -v5.3.29 application.Below is my Stored procedure in MySql

It outputs a total number of working days between two given days

Code(in PHP) to call this stored function is written below:

$db = dbLink();
$result = $db->query("SELECT WORKDAYS('2018-04-01','2018-04-08')");
if (!$result) {
   die('Could not query:' . mysql_error());
}
echo '<script>';
 echo 'console.log('. json_encode( $result ) .')';
echo '</script>'

Problem is when I try to call this stored function from a PHP application I get returned an object with Null attributes.

I'm writing the output '$result' on web console and the screenshots are attached below. $result object on web console

Upvotes: 0

Views: 301

Answers (1)

Nick
Nick

Reputation: 147206

Your problem is that $result is just the result set. You need to actually fetch the data from the result set. If you're using mysqli, use

$row = $result->fetch_array();
$workdays = $row[0];

if you're using PDO, you can get the result directly using

$workdays = $result->fetchColumn();

Upvotes: 1

Related Questions