random1 Posted July 19, 2010 Share Posted July 19, 2010 I have created this method within my database.php class. I'm using MySQL on my localhost PC. The code: public function executeQuery($sql, $type = '') { if($sql == '') { return false; } // Connection Variable $connection = $this->m_Connection; // Execute SQL using Connection (!empty($sql) && !empty($connection)) ? $result = $connection->multi_query($sql) : $result = ''; // Check if connection worked and no error was thrown if(!empty($connection) && mysqli_connect_errno() != 0 || $result == '') { // Unsuccessful $errorMessage = '**** mysqli error ' . mysqli_connect_errno() . ': ' . mysqli_connect_error(); error_log($errorMessage); throw new DatabaseBadSqlStatementException($errorMessage); return false; } // No errors and connection active else { // Successful $this->setDatabaseQueryCount($this->getDatabaseQueryCount() + 1); mysqli_next_result($connection); return true; } } Should the line: if(!empty($connection) && mysqli_connect_errno() != 0 || $result = '') instead be: if(!empty($connection) && mysqli_connect_errno() != 0 || $result == '') ? The particular stored procedure SQL I'm passing to this function works in Heidi SQL, a mysql editor, but not in my PHP. Other procedure calls work fine with it. Any ideas to fix it? Link to comment https://forums.phpfreaks.com/topic/208144-mysqli-and-multiple-stored-procedure-calls/ Share on other sites More sharing options...
random1 Posted July 19, 2010 Author Share Posted July 19, 2010 when I try it with the second line I get: Fatal error: Uncaught exception 'DatabaseBadSqlStatementException' with message '**** mysqli error 0: ' in ...\database.php:462 Stack trace: #0 It gives the error as 0 but no error message. The SQL I'm passing is: CALL `procedure_visit_insert`(12, 1, '6f7999c8a70ff5e52ebdc17b46b2386c9fea97198c31906d6d6f757ab7bf7c3244e3ef60401562b5e6ea6158fb58186dc29364f4edbeb80c1dc4dedbbcb7499c', 'Firefox', '3.6.6', 'Windows 7', '6.1', 1, 82, ; Link to comment https://forums.phpfreaks.com/topic/208144-mysqli-and-multiple-stored-procedure-calls/#findComment-1088017 Share on other sites More sharing options...
Mchl Posted July 19, 2010 Share Posted July 19, 2010 Try displaying mysqli_error not mysqli_connect_error() Link to comment https://forums.phpfreaks.com/topic/208144-mysqli-and-multiple-stored-procedure-calls/#findComment-1088020 Share on other sites More sharing options...
random1 Posted July 19, 2010 Author Share Posted July 19, 2010 From that I get the error: Commands out of sync; you can't run this command now Link to comment https://forums.phpfreaks.com/topic/208144-mysqli-and-multiple-stored-procedure-calls/#findComment-1088024 Share on other sites More sharing options...
Mchl Posted July 19, 2010 Share Posted July 19, 2010 You need to call mysqli_store_result to fetch entire result from previous query before you can fetch a result from the following one. Link to comment https://forums.phpfreaks.com/topic/208144-mysqli-and-multiple-stored-procedure-calls/#findComment-1088025 Share on other sites More sharing options...
random1 Posted July 19, 2010 Author Share Posted July 19, 2010 Where in my code would I need to add: mysqli_store_result($connection); Link to comment https://forums.phpfreaks.com/topic/208144-mysqli-and-multiple-stored-procedure-calls/#findComment-1088026 Share on other sites More sharing options...
Mchl Posted July 19, 2010 Share Posted July 19, 2010 If I understand correctly, this function should look something like this: public function executeQuery($sql, $type = '') { if(empty($sql))) { return false; } // Connection Variable $connection = $this->m_Connection; // Execute SQL using Connection if(!$connection->multi_query($sql)) { } do { if($connection->errno != 0) { throw new DatabaseBadSqlStatementException( '**** mysqli error '.PHP_EOL. $connection->error.PHP_EOL. $sql, $connection->errno ); } if($result = $connection->store_result()) { $this->setDatabaseQueryCount($this->getDatabaseQueryCount() + 1); //do something with result $result->free(); } } while ($connection->next->result()); return true; } Link to comment https://forums.phpfreaks.com/topic/208144-mysqli-and-multiple-stored-procedure-calls/#findComment-1088032 Share on other sites More sharing options...
random1 Posted July 19, 2010 Author Share Posted July 19, 2010 I ended up with: public function executeQuery($sql, $type = '') { if(empty($sql)) { return false; } // Connection Variable $connection = $this->m_Connection; mysqli_next_result($connection); // Execute SQL using Connection if(!$connection->multi_query($sql)) { // } do { if($connection->errno != 0) { /*throw new DatabaseBadSqlStatementException( '**** mysqli error ' .PHP_EOL. $connection->error.PHP_EOL.$sql, $connection->errno);*/ return false; } if($result = $connection->store_result()) { $this->setDatabaseQueryCount($this->getDatabaseQueryCount() + 1); //do something with result $result->free(); } } while(mysqli_next_result($connection)); return true; } This seems to working fine and I'll do more testing to get it just right Thanks Link to comment https://forums.phpfreaks.com/topic/208144-mysqli-and-multiple-stored-procedure-calls/#findComment-1088075 Share on other sites More sharing options...
Mchl Posted July 19, 2010 Share Posted July 19, 2010 Just noticed that this: if(!$connection->multi_query($sql)) { // } should in fact be $connection->multi_query($sql); Link to comment https://forums.phpfreaks.com/topic/208144-mysqli-and-multiple-stored-procedure-calls/#findComment-1088077 Share on other sites More sharing options...
random1 Posted July 20, 2010 Author Share Posted July 20, 2010 Thank you so much Mchl Link to comment https://forums.phpfreaks.com/topic/208144-mysqli-and-multiple-stored-procedure-calls/#findComment-1088445 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.