SoFunction
Updated on 2025-03-09

PHP database connection tool class [Definition and Usage] encapsulated by MySQL function

This article describes the database connection tool class encapsulated by PHP based on MySQL function. Share it for your reference, as follows:

<?php
class mysql
{
  private $mysqli;
  private $result;
  /**
    * Database connection
    * @param $config Configure array
    */
  public function connect($config)
  {
    $host = $config['host'];    //Host address    $username = $config['username'];//username    $password = $config['password'];//password    $database = $config['database'];//database    $port = $config['port'];    //Port number    $this->mysqli = new mysqli($host, $username, $password, $database, $port);
  }
  /**
    * Data query
    * @param $table data table
    * @param null $field field
    * @param null $where condition
    * @return mixed query results
    */
  public function select($table, $field = null, $where = null)
  {
    $sql = "SELECT * FROM {$table}";
    if (!empty($field)) {
      $field = '`' . implode('`,`', $field) . '`';
      $sql = str_replace('*', $field, $sql);
    }
    if (!empty($where)) {
      $sql = $sql . ' WHERE ' . $where;
    }
    $this->result = $this->mysqli->query($sql);
    return $this->result->num_rows;
  }
  /**
    * @return mixed Get all results
    */
  public function fetchAll()
  {
    return $this->result->fetch_all(MYSQLI_ASSOC);
  }
  /**
    * Insert data
    * @param $table data table
    * @param $data Data array
    * @return mixed Insert ID
    */
  public function insert($table, $data)
  {
    foreach ($data as $key => $value) {
      $data[$key] = $this->mysqli->real_escape_string($value);
    }
    $keys = '`' . implode('`,`', array_keys($data)) . '`';
    $values = '\'' . implode("','", array_values($data)) . '\'';
    $sql = "INSERT INTO {$table}( {$keys} )VALUES( {$values} )";
    $this->mysqli->query($sql);
    return $this->mysqli->insert_id;
  }
  /**
    * Update data
    * @param $table data table
    * @param $data Data array
    * @param $where filter conditions
    * @return mixed Affected records
    */
  public function update($table, $data, $where)
  {
    foreach ($data as $key => $value) {
      $data[$key] = $this->mysqli->real_escape_string($value);
    }
    $sets = array();
    foreach ($data as $key => $value) {
      $kstr = '`' . $key . '`';
      $vstr = '\'' . $value . '\'';
      array_push($sets, $kstr . '=' . $vstr);
    }
    $kav = implode(',', $sets);
    $sql = "UPDATE {$table} SET {$kav} WHERE {$where}";
    $this->mysqli->query($sql);
    return $this->mysqli->affected_rows;
  }
  /**
    * Delete data
    * @param $table data table
    * @param $where filter conditions
    * @return mixed Affected records
    */
  public function delete($table, $where)
  {
    $sql = "DELETE FROM {$table} WHERE {$where}";
    $this->mysqli->query($sql);
    return $this->mysqli->affected_rows;
  }
}

How to use

<?php
require_once '';
/* Configure connection parameters */
$config = array(
  'type' => 'mysql',
  'host' => 'localhost',
  'username' => 'woider',
  'password' => '3243',
  'database' => 'php',
  'port' => '3306'
);
/* Connect to the database */
$mysql = new mysql();
$mysql->connect($config);
/* Query data */
//1. Query all data$table = 'mysqli';//Data table$num = $mysql->select($table);
echo 'Queryed in total' . $num . 'Standard data';
print_r($mysql->fetchAll());
//2. Query some data$field = array('username', 'password'); //Filter field$where = 'id % 2 =0';          //Filter Conditions$mysql->select($table, $field, $where);
print_r($mysql->fetchAll());
/* Insert data */
$table = 'mysqli';//Data table$data = array(  //Data array  'username' => 'admin',
  'password' => sha1('admin')
);
$id = $mysql->insert($table, $data);
echo 'The ID of the insert record is' . $id;
/* Modify data */
$table = 'mysqli';//Data table$data = array(
  'password' => sha1('nimda')
);
$where = 'id = 44';
$rows = $mysql->update($table, $data, $where);
echo 'The number of affected records is' . $rows . 'strip';
/* Delete data */
$table = 'mysqli';
$where = 'id = 45';
$rows = $mysql->delete($table, $where);
echo 'Deleted' . $rows . 'Standard data';

For more information about PHP related content, please check out the topic of this site:Summary of php+mysqli database programming skills》、《PHP object-oriented programming tutorial》、《Complete collection of PHP array (Array) operation techniques》、《Introduction to PHP basic syntax》、《Summary of usage of php strings》、《PHP+mysql database operation tutorial"and"Summary of common database operation techniques for php

I hope this article will be helpful to everyone's PHP programming.