[Tutorial][Php] Establishing a MySQLi Connection.

Status
Not open for further replies.

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.
 

GarettM

Posting Freak
Aug 5, 2010
833
136
A Better TUT Would be ;3
Database Class :
This will control how you connect and use information from the mysql server
PHP:
<?php
 
#
// Connect to Mysqli object-oriented program style
#
 
  class sql {
      protected $mysqli; // declare property $mysqli
     
      public function __construct($host = 'HOST', $user = 'USER', $pass = 'PASSWORD', $data = 'DATABASE') {
          $this->mysqli = new mysqli($host, $user, $pass, $data);
         
          if($this->mysqli->connect_errno) {
              die("Fatal Error, Couldnt Connect to Mysql Server: " .  $this->mysqli->connect_error);
          }   
      }
      public function query($query) {
          return $this->mysqli->query($query);
      }
      public function __destruct() {
          $this->mysqli->close();
      }
  }
 
#
// Connect to Mysqli procedural program style
#
 
  $mysqli = mysqli_connect('HOST', 'USER', 'PASSWORD', 'DATABASE'); //declare object $mysqli;
 
  if(mysqli_connect_errno($mysqli)) {
        die("Fatal Error, Couldnt Connect to Mysql Server: " .  mysqli_connect_error($mysqli));
  }
 
  mysqli_query($mysqli, "SELECT * FROM *");
 
  mysqli_close($mysqli);
 
#
// BAD WAY procedural and object-oriented mixed [ NEVER DO ]
#
 
  $mysqli = mysqli_connect('HOST', 'USER', 'PASSWORD', 'DATABASE'); //declare object $mysqli;
 
  if(mysqli_connect_errno($mysqli)) {
        die("Fatal Error, Couldnt Connect to Mysql Server: " .  mysqli_connect_error());
  }
 
  $mysqli->close();
?>
 
Status
Not open for further replies.

Users who are viewing this thread

Top