Mysql connection class in PHP
This entry was posted in Just life and tagged connection, databases, Mysql, PHP, tutorial. Bookmark the permalink. Follow any comments here with the RSS feed for this post. Post a comment or leave a trackback: Trackback URL.
This is a tutorial to show you how to connect to a database from PHP and we will try to make a class to do all the dirty work for you.
In this tutorial you will learn how to use classes, functions and database connection in PHP.
To begin with, will create a new php file and we will declare a new class called in our example db_sql
class db_sql{}
Now in this class will create a function that will make the connection to our mysql server using some global variables that have to be defined in project : $db_link ,$db_server, $db_user, $db_pass, $db_name
function db_connect() { global $db_link ,$db_server, $db_user, $db_pass, $db_name ; $db_link = mysql_pconnect($db_server, $db_user, $db_pass) OR DIE ("I can't acces the server: " . mysql_error()); mysql_select_db($db_name) OR DIE ("I can't finde the database: " . mysql_error() . "") ; return TRUE ; } function db_close() { global $db_link ; if (isset($db_link)) { @mysql_close($db_link) ; unset($db_link) ; return TRUE ; } else { return FALSE ; } } function check($value) { if (get_magic_quotes_gpc()) { $value = stripslashes($value); } if (!is_numeric($value)) { $value = mysql_real_escape_string($value); } return $value; }
Now we have a sql connection. Our class can connect, disconnect and make an internal check for SQLInjection error. The next functions will demonstrate how insert, edit, delete or request elements from our database:
function insert($table,$rows,$values) { global $db_link; $result =""; $row = $value = array(); $row = split(",",$rows); $value = split(",",$values); if(count($row)==count($value)) { $sql = "INSERT INTO `".$table."` ("; for($i=0;$icheck($value[$i]))."',"; $sql .= "'".str_replace("}{",",",$this->check($value[$i]))."' );"; $result = $this->db_query($sql); } else { $result = "String incorect"; } return $result; } function edit($table,$rows,$values,$id) { global $db_link; $result =""; $row = $value = array(); $row = split(",",$rows); $value = split(",",$values); if(count($row)==count($value)) { $sql = "UPDATE `".$table."` SET "; for($i=0;$icheck($value[$i]))."',";} $sql .= "`".$row[$i]."` = "; $sql .= "'".str_replace("}{",",",$this->check($value[$i]))."' WHERE `id`='".$id."';"; $result = $this->db_query($sql); } else { $result = "String incorect"; } return $result; } function db_query($query) { global $db_link ; $result = mysql_query($query, $db_link) OR DIE ("Error:" . mysql_error() . "$query") ; return $result ; } function del($fild,$id) { global $db_link; $result= $this->db_query("DELETE FROM `$fild` WHERE id='$id'"); return $result; }
Finally this is all the code :
class db_sql{ function db_connect() { global $db_link ,$db_server, $db_user, $db_pass, $db_name ; $db_link = mysql_pconnect($db_server, $db_user, $db_pass) OR DIE ("Nu ma pot conecta la serverul: " . mysql_error()); mysql_select_db($db_name) OR DIE ("Nu gasesc baza de date: " . mysql_error() . "") ; return TRUE ; } function db_close() { global $db_link ; if (isset($db_link)) { @mysql_close($db_link) ; unset($db_link) ; return TRUE ; } else { return FALSE ; } } function check($value) { if (get_magic_quotes_gpc()) { $value = stripslashes($value); } if (!is_numeric($value)) { $value = mysql_real_escape_string($value); } return $value; } function insert($table,$rows,$values) { global $db_link; $result =""; $row = $value = array(); $row = split(",",$rows); $value = split(",",$values); if(count($row)==count($value)) { $sql = "INSERT INTO `".$table."` ("; for($i=0;$icheck($value[$i]))."',"; $sql .= "'".str_replace("}{",",",$this->check($value[$i]))."' );"; $result = $this->db_query($sql); } else { $result = "String incorect"; } return $result; } function edit($table,$rows,$values,$id) { global $db_link; $result =""; $row = $value = array(); $row = split(",",$rows); $value = split(",",$values); if(count($row)==count($value)) { $sql = "UPDATE `".$table."` SET "; for($i=0;$icheck($value[$i]))."',";} $sql .= "`".$row[$i]."` = "; $sql .= "'".str_replace("}{",",",$this->check($value[$i]))."' WHERE `id`='".$id."';"; $result = $this->db_query($sql); } else { $result = "String incorect"; } return $result; } function db_query($query) { global $db_link ; $result = mysql_query($query, $db_link) OR DIE ("Error:" . mysql_error() . "$query") ; return $result ; } function del($fild,$id) { global $db_link; $result= $this->db_query("DELETE FROM `$fild` WHERE id='$id'"); return $result; } }
Now you will have to include this file in your project, define $db_link ,$db_server, $db_user, $db_pass, $db_name , make an instance of this class and do all your dirty work with Mysql databases.
include("classMysql.php"); db_server="localhost"; db_user="your database username"; db_pass="your database password"; db_name="your database name"; db_sql test = new db_sql(); test->insert("users","username,password","stefa".MD5(1234));
Comments
Post a Comment