PHP Classes

DB Connection: Connect to MySQL and store connections in sessions

Recommend this page to a friend!
     
  Info   Example   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not enough user ratingsTotal: 184 All time: 8,672 This week: 455Up
Version Licenses PHP version Categories
db-connection 1.3GNU Free Document...5.2PHP 5, Databases
Description 

Author

This is a simple class that can connect to MySQL and store connections in sessions.

It can connect to a given MySQL database server using MySQLi and optionally store the connection in a session variable.

The class can also disconnect from the database and clear the respective session variable if it was set.

Picture of Daniel Alan Guerrero Matamoros
  Performance   Level  
Name: Daniel Alan Guerrero ... <contact>
Classes: 7 packages by
Country: Mexico Mexico
Innovation award
Innovation award
Nominee: 1x

Example

<?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

?>


  Files folder image Files (2)  
File Role Description
Plain text file db_connect.php Class Main Class of DB connection
Accessible without login Plain text file db_connection_example.php Example Simple example of usage

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 0%
Total:184
This week:0
All time:8,672
This week:455Up