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(){   
  * Use mysqli_connect instead of deprecated mysql_connect
  function connect(){   
    $this->conn_id = mysqli_connect($this->host, $this->user, $this->password); 
    if(!$this->conn_id ){   
  print_r("<br /> NO MySQL connection <br />Check your username and pass ");       
  //change to another one if you want
  $status = mysqli_select_db($this->conn_id,$this->database);
     print_r("NO database Connection <br /> Check your database name");             
  function query($sql){  
 //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);    
  //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){
      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.

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
Compile MySQL in Ubuntu


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