How to

PHP Class for MySQL (for beginners)

MySQL database is one of the widely available and used Database engines, so are the variety of programming languages and scripts used by software engineer or programmer to access and manage it.

Here is a simple and portable PHP class to access MySQL database. You can use it to start learning and testing as well.

/**
* This is simply a beginners guide to
* PHP programming to connect MySQL database
* It is a simple  class of PHP that can be used to
* connect and query MySQL.
* I have used MySQLi procedural way, so any beginner
* will can learn and study.
* Usage:
* 1- Call your PHP class
*  $mydb = new MySQLiConn();
* 2- create your query (like select, update, delete, etc)
*  $sql="SELECT * FROM mytable";
* 3- Start querying (by calling query function)
*  $mydb->query($sql);
* 4- Display results by calling functions like
*  while($mydb->next_record()){
*   echo $mydb->get_field("column name to display");
*  }
* Note: To query you have to use safe query methods, so
* MySQL wouldn't complain
*
*/

  
class MySQLiConn{
 
 
  private $conn_id;
  private $query_id;
  private $record;
  //where the MySQL
  private $host="localhost";
  //change to the name of your database
  private $database="database_name";
  //set your own access password
  private $password="your password";
  //your username of database access
  private $user="database user";
 
  // PHP class
  function __construct(){   
 $this->connect();    
  }
  /**
  * Use mysqli_connect instead of deprecated mysql_connect
  */ 
  function connect(){   
   mb_language('uni');
 mb_internal_encoding('UTF-8');   
    $this->conn_id = mysqli_connect($this->host, $this->user, $this->password); 
    if(!$this->conn_id ){   
  echo(mysqli_errno().":".mysqli_error());
  print_r("<br /> NO MySQL connection <br />Check your username and pass ");       
       exit;    
 } 
    else{
  //change to another one if you want
       mysqli_set_charset($this->conn_id,'utf8');
  $status = mysqli_select_db($this->conn_id,$this->database);
    
       if(!$status){
   echo(mysqli_errno($this->conn_id).":".mysqli_error($this->conn_id));
     print_r("NO database Connection <br /> Check your database name");             
         exit;        
       }
  mysqli_set_charset($this->conn_id,'utf8');
 }
  } 
 
  function query($sql){  
   $sql=mb_convert_encoding($sql,'UTF-8','UTF-8');
 //first check MySQL connection, if not, reconnect it
    if(empty($this->conn_id))    $this->connect();
 //if connected, do the query
    $this->query_id = mysqli_query($this->conn_id,$sql );  
    if(!$this->query_id ){ // if there is somthing wrong   
  //check errors and find where the error originates from your query
  echo(mysqli_errno($this->conn_id).":".mysqli_error($this->conn_id)."<br />".$sql);
  print_r("<br />Error lies with this query <br /> ".$sql);    
       exit;   
    } 
  } 
  //MySQL query (when selected) will fetch array of records 
  function next_record(){
    $this->record = mysqli_fetch_array($this->query_id);
    $status = is_array($this->record);
    return $status;
  }
  function next_row(){
   $this->record = mysqli_fetch_assoc($this->query_id); 
      return $this->record;
  }
  //SELECT, INSERT, UPDATE and DELETE need different ways 
  function count_rows($choice){
   $rows=0;
   //for UPDATE, INSERT and DELETE
      if($choice=='5')$rows = mysqli_affected_rows($this->conn_id);
   //for SELECT
   if($choice=='3')$rows = mysqli_num_rows($this->query_id);
    return $rows;
  }
 
  function get_field($field){  
   return $this->record[$field];
  }
  function close(){   
   return mysqli_close($this->conn_id);
  } 
 
}


Comments (2)
A Muhammad

I am a beginner, and when I copied your code there is a lot of errors. if we can download real code will be good.
It is a clear and easy code and thank you.
Lorenz

Thank you for your time. iam want to make lot of practice, but am getting errors, can you give us a link to download the code

Add a Comment

-->

How to

Hard Disk
Clone Hard Disk with different sector size
MySQL
Compile MySQL in Ubuntu

Products

We have ready-made CMS sytems that can be edited to suit your needs. We gurantee that our sytems can be administered or managed by most people who have basic IT knowledge. Also, we have
  • Applications and software for specialised domains and tasks
  • New and used Computers and laptops