Whatsapp

Php mysql in one line


Hello cool programmers today in this blog I will share my crud class script so that you can save your time while writing php mysql codes. Using this script you can save your time.
So let's start 

Downloading database.php file

Click on the below button to download the file. 





Editing database credentials in database.php file

Open the file after downloading and edit some basic database credentials.

<?php
class db
{
    private $host = 'localhost';
    private $user = "root";
    private $password = "";
    private $db = "database_name";
    .
    .
    .
?>


Including and creating object

Now include the file in your program and create its class. If the there is any error while connecting to database then it will show error : ' Could not connect to database - Error : <ERROR> '

  <?php
require_once "database.php";
$db=new db();
?>





Inserting / updating / Deleting

<?php
require_once "database.php";
$db=new db();
$res=$db->basic("YOUR_SQL_QUERY");
if($res==1){
    echo "Operation Successfull !";
}
else{
    echo $res;
}
?>


Selecting

Here select() method will return 0 if no data is present, error when there is any error and if it is successfull it will return 2-D Array.

<?php
require_once "database.php";
$db=new db();
$res=$db->select("YOUR_SELECT_QUERY");
$row=$db->rows;
for($i=0;$i<=$row;$i++){
    echo $res[$i][COLUMN_NAME];
}
?>


Encryption / Decryption

<?php
require_once "database.php";
$db=new db();
echo $e=$db->encrypt("text to encrypt");
echo $d=$db->decrypt("encrypted text to decrypt");
?>


Encryption / Decryption with key

<?php
require_once "database.php";
$db=new db();
echo $e=$db->encrypt("text to encrypt","any_random_text");
echo $d=$db->decrypt("encrypted text to decrypt","any_random_text");
?>


Login system

If data is present in database then it will return 1 otherwise 0 and if any error occures then it will return error. It takes one optional perimeter that is any one column of table to create a cookie with it's value if no perimeter is passed the login cookie will store 1.

<?php
require_once "database.php";
$db=new db();
$e=$db->login("SELECT_QUERY","optional_perimeter_column_name");//EX:- select email,pass where email='' and pass='';
if($e==1){
    echo $_COOKIE['login'];
    echo "Login successful !";
}
else if($e==0){
    echo "Details not matched !";
}
else{
    echo $e;
}
?>

Hope you have enjoyed if you have any problem message me on instagram @onkarjha2003.

Post a Comment

0 Comments

Ad Code

Responsive Advertisement