Hello
I try to handle mysqli exception.
After reading this article <!-- m --><a class="postlink" href="http://www.zend.com/php5/articles/php5-mysqli2.php?article=php5-mysqli2&kind=php5&id=5168&open=&anc=0&view=1">http://www.zend.com/php5/articles/php5- ... c=0&view=1</a><!-- m --> I have try the code of the author but it didn't work !
When you try to instanciate the mysqli class by doing :
parent::__construct($adr, $login, $mdp, $nom_bd)
it always return this error :
mysqli::mysqli() [function.mysqli]: Unknown MySQL server host 'loclhost' (11001) in c:\wamp\www\wmi\mysqli.php on line 16
Connection Error
With exception in other language like ASP.NET it works well, it should print me only "Connection Error" but he doesn't
How to handle mysql exception correctly with php ?
<?php
/* Create custom exception classes */
class ConnectException extends Exception {}
class QueryException extends Exception {}
class example_mysqli extends mysqli
{
function __construct($adr, $login, $mdp, $nom_bd)
{
try{
/* Pass all arguments passed to the constructor on to the parent's constructor */
$args = func_get_args();
// OK if(!@parent::__construct($adr, $login, $mdp, $nom_bd))
if(!parent::__construct($adr, $login, $mdp, $nom_bd))
{
throw new ConnectException(mysqli_connect_error(), mysqli_connect_errno());
}
/* Throw an error if the connection fails */
if(mysqli_connect_error()){
throw new ConnectException(mysqli_connect_error(), mysqli_connect_errno());
}
}
catch (Exception $e)
{
throw new ConnectException(mysqli_connect_error(), mysqli_connect_errno());
}
}
function query($query)
{
$result = parent::query($query);
if(mysqli_error($this)){
throw new QueryException(mysqli_error($this), mysqli_errno($this));
}
return $result;
}
}
try {
$my = new example_mysqli('loclhost','root', '', 'test');
$result = $my->query("SELCT NOW()");
var_dump($result->fetch_row());
}
catch(Exception $exception) {
echo "Connection Error\n";
//var_dump($exception->getMessage());
}
catch(QueryException $exception) {
echo "Query Error\n";
//var_dump($exception->getMessage());
}
/* Handle exceptions that we weren't expecting */
catch(Exception $exception) {
echo "Who was that masked exception?\n";
//var_dump($exception->getMessage());
}
$result->close();
$my->close();
?>Put this at the top of your code if you don't want to see PHP's errors and warnings.
error_reporting(0);No I want to handle error like I do in other language.
If there is a connection problem I want to throw a mysqli_connection_exception if there is a query exception I want to throw a mysqli_query_exception and so on.
Exceptin are made to handle error so disabling error is not the solution :queasy:The above line of code will disable PHP's errors, not your custom exceptions.Originally posted by tidou
Exceptin are made to handle error so disabling error is not the solution :queasy:
There's a fundamental difference between errors and Exceptions as far as PHP is concerned. Errors are typically just strings printed to the screen, and are handled in a custom way by using set_error_handler(). Exceptions are objects that can be custom-handled by using either try...catch, or set_exception_handler(). The latter of the Exception handlers is a safety net to handle uncaught Exceptions.
Another solution, since you are fetching the error via mysqli_connect_error(), is to put an @ in front of your call to the parent constructor. That way you can suppress any built-in PHP error for that call since you are handling it yourself, and still evaluate your object/FALSE condition.
You had mentioned ASP.NET exceptions... in .NET, there is no differentiation between an "error" and "exception". What you raise will be printed to screen if uncaught, and all internal .NET libraries/assemblies generate exceptions in the same way, unlike the PHP extensions which vary in how they signal a problem (some use Exceptions, most use plain old errors)You had mentioned ASP.NET exceptions... in .NET, there is no differentiation between an "error" and "exception". What you raise will be printed to screen if uncaught, and all internal .NET libraries/assemblies generate exceptions in the same way, unlike the PHP extensions which vary in how they signal a problem (some use Exceptions, most use plain old errors)
yes and doing so is a lot easyer PHP's Exceptions work pretty much like Java (and presumably C# etc). So you should be able to write PHP Exceptions exactly as you would in those other languages. The only difference is that PHP can be told to generate additional warnings and errors if necessary (useful during development, not useful after deployment).
I try to handle mysqli exception.
After reading this article <!-- m --><a class="postlink" href="http://www.zend.com/php5/articles/php5-mysqli2.php?article=php5-mysqli2&kind=php5&id=5168&open=&anc=0&view=1">http://www.zend.com/php5/articles/php5- ... c=0&view=1</a><!-- m --> I have try the code of the author but it didn't work !
When you try to instanciate the mysqli class by doing :
parent::__construct($adr, $login, $mdp, $nom_bd)
it always return this error :
mysqli::mysqli() [function.mysqli]: Unknown MySQL server host 'loclhost' (11001) in c:\wamp\www\wmi\mysqli.php on line 16
Connection Error
With exception in other language like ASP.NET it works well, it should print me only "Connection Error" but he doesn't
How to handle mysql exception correctly with php ?
<?php
/* Create custom exception classes */
class ConnectException extends Exception {}
class QueryException extends Exception {}
class example_mysqli extends mysqli
{
function __construct($adr, $login, $mdp, $nom_bd)
{
try{
/* Pass all arguments passed to the constructor on to the parent's constructor */
$args = func_get_args();
// OK if(!@parent::__construct($adr, $login, $mdp, $nom_bd))
if(!parent::__construct($adr, $login, $mdp, $nom_bd))
{
throw new ConnectException(mysqli_connect_error(), mysqli_connect_errno());
}
/* Throw an error if the connection fails */
if(mysqli_connect_error()){
throw new ConnectException(mysqli_connect_error(), mysqli_connect_errno());
}
}
catch (Exception $e)
{
throw new ConnectException(mysqli_connect_error(), mysqli_connect_errno());
}
}
function query($query)
{
$result = parent::query($query);
if(mysqli_error($this)){
throw new QueryException(mysqli_error($this), mysqli_errno($this));
}
return $result;
}
}
try {
$my = new example_mysqli('loclhost','root', '', 'test');
$result = $my->query("SELCT NOW()");
var_dump($result->fetch_row());
}
catch(Exception $exception) {
echo "Connection Error\n";
//var_dump($exception->getMessage());
}
catch(QueryException $exception) {
echo "Query Error\n";
//var_dump($exception->getMessage());
}
/* Handle exceptions that we weren't expecting */
catch(Exception $exception) {
echo "Who was that masked exception?\n";
//var_dump($exception->getMessage());
}
$result->close();
$my->close();
?>Put this at the top of your code if you don't want to see PHP's errors and warnings.
error_reporting(0);No I want to handle error like I do in other language.
If there is a connection problem I want to throw a mysqli_connection_exception if there is a query exception I want to throw a mysqli_query_exception and so on.
Exceptin are made to handle error so disabling error is not the solution :queasy:The above line of code will disable PHP's errors, not your custom exceptions.Originally posted by tidou
Exceptin are made to handle error so disabling error is not the solution :queasy:
There's a fundamental difference between errors and Exceptions as far as PHP is concerned. Errors are typically just strings printed to the screen, and are handled in a custom way by using set_error_handler(). Exceptions are objects that can be custom-handled by using either try...catch, or set_exception_handler(). The latter of the Exception handlers is a safety net to handle uncaught Exceptions.
Another solution, since you are fetching the error via mysqli_connect_error(), is to put an @ in front of your call to the parent constructor. That way you can suppress any built-in PHP error for that call since you are handling it yourself, and still evaluate your object/FALSE condition.
You had mentioned ASP.NET exceptions... in .NET, there is no differentiation between an "error" and "exception". What you raise will be printed to screen if uncaught, and all internal .NET libraries/assemblies generate exceptions in the same way, unlike the PHP extensions which vary in how they signal a problem (some use Exceptions, most use plain old errors)You had mentioned ASP.NET exceptions... in .NET, there is no differentiation between an "error" and "exception". What you raise will be printed to screen if uncaught, and all internal .NET libraries/assemblies generate exceptions in the same way, unlike the PHP extensions which vary in how they signal a problem (some use Exceptions, most use plain old errors)
yes and doing so is a lot easyer PHP's Exceptions work pretty much like Java (and presumably C# etc). So you should be able to write PHP Exceptions exactly as you would in those other languages. The only difference is that PHP can be told to generate additional warnings and errors if necessary (useful during development, not useful after deployment).