PHP Classes

File: db_connection_example.php

Recommend this page to a friend!
  Classes of Daniel Alan Guerrero Matamoros   DB Connection   db_connection_example.php   Download  
File: db_connection_example.php
Role: Example script
Content type: text/plain
Description: Simple example of usage
Class: DB Connection
Connect to MySQL and store connections in sessions
Author: By
Last change: All type of connection stablished
Date: 9 years ago
Size: 1,000 bytes
 

Contents

Class file image Download
<?php

session_start
();

require_once (
"db_connect.php");

#we create a simple connect without specify the db to connect

$simple = new db_connect("localhost", "my_user", "my_pass");
$simple_con = $simple->connect(); #we receive the connection variable
#we make our sql statements and procedures as normal
#blabla
#blabla
#blabla
$simple->disconnect();

#create connection specifying the data base

$database = new db_connect("my_host", "my_user", "my_pass", "my_db");
$database_con = $database->connect();
#our sql statements
#do
#la
#sol
#re
#mi
#fa
$database->disconnect();

#if we want to save the connection in a session variable we use:
$s = new db_connect("my_host", "my_user", "my_pass", "my_db", true);
#we don't receive the connection variable, the session is stored
#in: $_SESSION["conn"]
$s->connect();
#our sql statements
#bleu
#bleu
#l'amour
#est bleu
$s->disconnect(); #the session variable value is now false
#and the connection is closed

?>