Tuesday, 9 June 2020

how to tell if it is not connected to server and data base in try , throw and catch

how to tell if it is not connected to server and data base in try , throw and  catch
<?php

$host = 'localhost';
$user = 'root';
$pass = '';
$dbname = 'mydatabase';

to use for multiple execption use extend
class ServerException extends Exception{}
class DbException extends Exception{}

we have two errors, to connect serverd and to connect database

try{
try to connect to server, if it sonnectd , it will throw a error , that error catched by catcha dn execute what the argument it passes, if connected to serverd, not conencted to database, try too conect to databse, if its not connet to database, throw a error with argument, it iwll catched by ,it execute that error and prints that
if ($mycon = @mysqli_connect($host, $user, $pw)){
if (@mysqli_select_db($mycon, $dbname)){echo 'connected';}

 else{throw new DbExcption('could not slect data base');  }
if it is not connected database, tells could not coonect database


}else
{
throw new ServerException (' could not connect to the server');
if server is not connected tell this
}
}catch( ServerException $ex)
ex is a instance
{ echo 'Error:'.$ex-> getMessage();
}
catch(DbException $ex)
{
echo 'errpr:'.$ex -> getmessage();
}


?>

class extends
class extends
try
if
if
els
else
throw new
catch
catch
how to tell if it is not connected to server and data base in try , throw and  catch
how to use multiple execptions
how to use try throw catch



No comments:

Post a Comment