PHP Function to Connect to SQL Database
PHP Function to Connect to SQL Database and Return Handle to Database for Further Manipulation or Return False if mysqli_connect_error()
Home
Short:
.
/* ----------------------------------------------- open database and return handle to database if database is opened with no errors or false ---------------------------------------------- */ function openDatabase($dbPath = "localhost", $dbUser, $dbPass, $dbName) { // open database. $dbconn = mysqli_connect( $dbPath, // path to db $dbUser, // db username $dbPass, // db password $dbName // db name ); // if database connection fails if (mysqli_connect_errno()) { $err = mysqli_connect_error(); // error handling code goes here; return false; } return $dbconn; }
source code home