NameSilo

Simple MySQL connect class

Spaceship Spaceship
Watch

Eric

VIP Member
Impact
328
Simple MySQL connect class:

PHP:
<?php

class MySQL_Connect
{
  var $link;
  //MySQL Database Password
  var $db_pass;
  //MySQL Database Username
  var $db_user;
  //MySQL Host Name
  var $db_host = 'localhost';
  //MySQL Database Name
  var $db_name;

  /*
  Connects to the SQL db, and selects the db
  NOTE: You must call set_sql_info() first
  */
  function connect()
  {
    $this->link = mysql_connect($this->db_host, $this->db_user, $this->db_pass) or 

die(mysql_error());

    if(!$this->link)
    {
      return false;
    }

    if(mysql_select_db($this->db_name, $this->link) or die(mysql_error()))
    {
      return true;
    }
    return false;
  }

  /*
  Closes the MySQL connection
  */
  function close()
  {
    mysql_close($this->link);
  }

  /*
  Sets the MySQL Info
  */
  function set_sql_info($db_user, $db_pass, $db_name, $db_host='localhost')
  {
    $this->db_pass = $db_pass;
    $this->db_user = $db_user;
    $this->db_host = $db_host;
    $this->db_name = $db_name;
  }
}
?>

Usage:

PHP:
<?php

include("MySQL_Connect.php");

//Initialize class
$db = new MySQL_Connect; 

//Set db username, password, and database name
$db->set_sql_info("username",  "password",  "database_name");

//Connect to sql
$db->connect();

//Whatever

//Close the connection
$db->close();

?>
 
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
GoDaddyGoDaddy
Very Nice Eric!

Should be noted that the top part needs to be directly part of the same document you are using the class in, or it needs to be included as seen below:

PHP:
<?
include "mysql_connect_class.php"; 
?>

-Steve
 
0
•••

We're social

Unstoppable Domains
Domain Recover
DomainEasy — Live Options
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back