DaLightz
See ya'll in the afterlife.
- May 19, 2012
- 1,136
- 262
So,first we have to make a configuration file so the connection can read where to connect to.
Config.php
PHP:
<?php
// Just so it's more neat instead of something like $host, we will do $_CONFIG['MySQLi]['Hostname'].
$_CONFIG['MySQLi']['Hostname'] = 'localhost'; # This is usually always localhost, unless you are accessing an external MySQLi server.
$_CONFIG['MySQLi']['Username'] = 'root'; # This is the username that is required for your MySQLi service. (e.g, PhpMyADMIN).
$_CONFIG['MySQLi']['Password'] = 'lol?'; # Need I say?
$_CONFIG['MySQLi']['Database'] = 'lol.'; # Database name.
// Done!
?>
Now the connection bit.
MySQLi.php
PHP:
<?php
// Require your config file.
require_once 'Config.php';
// Establishing the actual connection, and setting the MySQLi variable.
$mysqli = new mysqli($_CONFIG['MySQLi']['Hostname'], $_CONFIG['MySQLi']['Username'], $_CONFIG['MySQLi']['Password'], $_CONFIG['MySQLi']['Database']);
// But... what if the connection don't work? How will we know?
// Here's how.
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}else{
// Success Message.
echo 'Worked!';
}
// English:
// If mysql error = true, echo error message. Or else, display success message.
?>
Sorry it came out weird, I copied it from my post on another forum.