PHP MySQL Using OOPs
Working with PHP MySQL Using OOPS
In this blog, you are going to learn how to connect with MySQL database and perform different CRUD operations using Oops based class in PHP.
This tutorial explains to you how to create a class with some functions that are used to maintain the database.
The below given example class having some methods and uses those methods in test class to perform various operations such as listing all the users, testing admin login, deleting a record and so on.
1. Create a table - users with following fields:
- id
- username
- password
- email
2. Create dbclass.php and paste the given code below and save in some folder in server.
This class having the following functions:
- dbconn()
- dbclose()
- dbselect()
- dblogin($user,$pwd)
- dbdelete($user)
- dbupdate($username,$oldpassword,$newpassword)
3. Create another file dbclasstest.php as given below and run that file in a localhost.
Note: Change the db details according to yours in dbclass.php in order to connect with database.
dbclass.php
<?php
class Dbclass{
public $conn = "";
public $dbfound = "";
public $SQL = "";
public $result = "";
private function dbconn(){
$this->conn =
mysqli_connect("localhost","root","","usersdb")
or die("Failed to connect to Database " . mysqli_error());
}
function dbclose(){
mysqli_close($this->conn);
}
function dbselect($table)
{
$this->dbconn();
$this->SQL = "SELECT * FROM ".$table;
$this->result = mysqli_query($this->conn,$this->SQL);
while ($db_fields = mysqli_fetch_assoc($this->result))
{
print $db_fields['id'] . " ".
print $db_fields['username'] . " ";
print $db_fields['password'] . "<br>";
}
$this->dbclose();
}
function dblogin($user,$pwd)
{
$this->dbconn();
$this->SQL = "select * from users where username='$user'
and password='$pwd'";
$this->result=mysqli_query($this->conn,$this->SQL);
$num_rows = mysqli_num_rows($this->result);
if ($num_rows > 0) {
return "<br />success";
}
else
{
return "<br />Invalid Login";
}
$this->dbclose();
}
function dbdelete($user)
{
$this->dbconn();
$this->result =mysqli_query($this->conn,"SELECT * from
users where username='$user'");
$num_rows=mysqli_num_rows($this->result);
if($num_rows==0)
{
die ('<br /> Could not find User');
}
else{
mysqli_query($this->conn,"DELETE FROM users WHERE
username='$user'") or die
(mysqli_error());
echo "<h1>Record Successfully Deleted..!!</h1>";
}
$this->dbclose();
}
function dbupdate($username,$oldpassword,$newpassword){
$this->dbconn();
$this->SQL = "UPDATE users SET password = '$newpassword'
WHERE
password='$oldpassword' AND username = '$username'";
//echo "<br /> mysqli_info". mysqli_info($this->conn);
$this->result = mysqli_query($this->conn,$this->SQL);
//echo "<br />affected rows: ".mysqli_affected_rows();
if(mysqli_affected_rows()> 0){
print "<br />Records updated to the database";
$this->dbclose();
}
else{
echo "<br />No recards found ";
}
}
}
?>
The below program shows how to create the above class object and make use of those methods.
dbclasstest.php
<?php
include("dbclass.php");
$dbobj=new Dbclass();
$dbobj->dbselect("users");
echo $dbobj->dblogin("admin","admin");
$dbobj->dbdelete("admin");
$dbobj->dbselect("users");
//$dbobj->dbupdate("bill","gates","gates1");
//$dbobj->dbselect("users");
?>
Comments
Post a Comment