Home Backend Development PHP Tutorial mysql class comments throughout

mysql class comments throughout

Aug 08, 2016 am 09:32 AM
gt public quot this

Profile Section

<?php
/**
 * Created by wangsir
 * User: Administrator
 * Date: 14-12-22
 * Time: 上午9:29
 */

define(&#39;DB_MS&#39;,&#39;mysql&#39;);            //服务器
define(&#39;DB_USER&#39;,&#39;root&#39;);           //数据库用户名
define(&#39;DB_PASSWD&#39;,&#39;111&#39;);          //数据库密码
define(&#39;DB_NAME&#39;,&#39;bbs&#39;);            //使用数据库
define(&#39;DB_HOST&#39;,&#39;localhost&#39;);      //数据库服务器
define(&#39;DB_COD&#39;,&#39;utf8&#39;);            //数据库编码
define(&#39;DB_DSN&#39;,DB_MS.&#39;:host=&#39;.DB_HOST.&#39;;dbname=&#39;.DB_NAME);
Copy after login

Class
<?php
/**
 * Created by wangsir.
 * Date: 14-12-22
 * Time: 上午9:22
 * 本扩展自 PHP 5.5.0 起已废弃,并在将来会被移除。
 */


header("Content-Type:text/html;charset=utf-8");
//包含配置文件
require_once("config.ini.php");

class MySqli
{
    public $conn;            //保存数据库连接资源
    public $host;            //服务器
    public $username;        //数据库用户名
    public $password;        //数据密码
    public $dbname;          //数据库名
    public $cod;             //数据库编码

    public $dataTable;       //数据库表
    public $sql;             //sql语句
    public $where;           //条件
    public $limit;           //几条数据
    public $select;          //查询

    //数据库连接
    //这里采用读取配置文件的方式获取基本信息 也可以在构造函数传数组来解决同样的问题
    public function __construct($host=DB_HOST ,$username=DB_USER,$passwd=DB_PASSWD,$dbname=DB_NAME,$cod=DB_COD)
    {
        //获取数据库长连接,因为有长连接了所以就不在做单例模式
        $this->conn = new mysqli($host,$username,$passwd);
        $this->selectDb($dbname);
        $this->coding($cod);
    }

    //设置编码格式
    public function coding($cod)
    {
        mysql_query("SET CHARACTER SET ".$cod);
    }

    //读取某个库
    public function selectDb($dbname)
    {
        mysql_select_db($dbname,$this->conn);
    }

    //要查询什么字段 这个事查询的最后一步
    public function select($field="*")
    {
        $this->sql = "select ".$field." from ".$this->dataTable.$this->where.$this->limit;

    }

    //要使用哪个表
    public function table($tableName)
    {
        $this->dataTable = $tableName;
        return $this;
    }

    //执行操作
    public function query()
    {
        $res = mysql_query($this->sql);

        //是资源类型代表查询 返回结果数组
        if(is_resource($res))
        {
            //将返回二维数组
            while($row = mysql_fetch_assoc($res))
            {
                $arr[] = $row;
            }
            //返回一维数组
            foreach($arr as $key=>$value)
            {
                $ke = $value;
            }
            return $ke;
        }

    }

    //取得几个值 limit
    public function limit($limit)
    {
        $this->limit = ' limit '.$limit;
        return $this;
    }

    //where条件
    public function where($where)
    {
        $this->where = ' where '.$where;
        return $this;
    }

    //insert
    public function insert($insert)
    {
        //如果是索引数组
        if($this->is_assoc($insert))
        {
            //获取数组的key 也就是字段值
            $key = array_keys($insert);
            $value = array_values($insert);
            for($i=0,$j=0;$i<count($key),$j<count($value);$i++,$j++)
            {
                $column.=$key[$i].&#39;,&#39;;
                $val.=$value[$j].&#39;,&#39;;
            }
            $col = rtrim($column,&#39;,&#39;);
            $value = rtrim($val,&#39;,&#39;);
            $this->sql = "insert into ".$this->dataTable."($col) values($value)";

        }else{
            //如果不是索引数组 那么直接将数组转化为字符串
            $value = implode(',',$insert);
            $this->sql = "insert into ".$this->dataTable." values($value)";
        }
    }

    //修改
    public function update($update)
    {
        foreach($update as $key=>$value)
        {
            $data.=$key."=".$value.',';
        }
        $values = rtrim($data,',');
        $this->sql = "UPDATE ".$this->dataTable." SET $values ".$this->where;
    }

    //删除
    public function delete()
    {
        $this->sql = "DELETE FROM ".$this->dataTable.$this->where;
    }

    //判断是否为索引数组
    public function is_assoc($array) {
        if(is_array($array)) {
            $keys = array_keys($array);
            return $keys != array_keys($keys);
        }
        return false;
    }

}


//$b = array('CategoryID'=>9,'Name'=>11,'Description'=>22,'Priority'=>35);
//$b = array(4,5,6,7);
//$a = new MySqli();
//查询
//$sql = $a->table("guagua_category")->where('CategoryID=1')->limit(3)->select();
//$arr = $a->query();

//插入
//$sql = $a->table("guagua_category")->insert($b);
//$a->query();

//修改
//$sql = $a->table("guagua_category")->where("CategoryID=1")->update($b);
//$a->query();
//删除
//$sql = $a->table("guagua_category")->where("CategoryID=9")->delete();
//$a->query();
Copy after login

The above introduces the mysql class and comments throughout the article, including aspects of the content. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What are the differences between Huawei GT3 Pro and GT4? What are the differences between Huawei GT3 Pro and GT4? Dec 29, 2023 pm 02:27 PM

Many users will choose the Huawei brand when choosing smart watches. Among them, Huawei GT3pro and GT4 are very popular choices. Many users are curious about the difference between Huawei GT3pro and GT4. Let’s introduce the two to you. . What are the differences between Huawei GT3pro and GT4? 1. Appearance GT4: 46mm and 41mm, the material is glass mirror + stainless steel body + high-resolution fiber back shell. GT3pro: 46.6mm and 42.9mm, the material is sapphire glass + titanium body/ceramic body + ceramic back shell 2. Healthy GT4: Using the latest Huawei Truseen5.5+ algorithm, the results will be more accurate. GT3pro: Added ECG electrocardiogram and blood vessel and safety

Fix: Snipping tool not working in Windows 11 Fix: Snipping tool not working in Windows 11 Aug 24, 2023 am 09:48 AM

Why Snipping Tool Not Working on Windows 11 Understanding the root cause of the problem can help find the right solution. Here are the top reasons why the Snipping Tool might not be working properly: Focus Assistant is On: This prevents the Snipping Tool from opening. Corrupted application: If the snipping tool crashes on launch, it might be corrupted. Outdated graphics drivers: Incompatible drivers may interfere with the snipping tool. Interference from other applications: Other running applications may conflict with the Snipping Tool. Certificate has expired: An error during the upgrade process may cause this issu simple solution. These are suitable for most users and do not require any special technical knowledge. 1. Update Windows and Microsoft Store apps

What is the difference between the developer version and the public version of iOS? What is the difference between the developer version and the public version of iOS? Mar 01, 2024 pm 12:55 PM

Every year before Apple releases a new major version of iOS and macOS, users can download the beta version several months in advance and experience it first. Since the software is used by both the public and developers, Apple has launched developer and public versions, which are public beta versions of the developer beta version, for both. What is the difference between the developer version and the public version of iOS? Literally speaking, the developer version is a developer test version, and the public version is a public test version. The developer version and the public version target different audiences. The developer version is used by Apple for testing by developers. You need an Apple developer account to download and upgrade it.

How to Fix Can't Connect to App Store Error on iPhone How to Fix Can't Connect to App Store Error on iPhone Jul 29, 2023 am 08:22 AM

Part 1: Initial Troubleshooting Steps Checking Apple’s System Status: Before delving into complex solutions, let’s start with the basics. The problem may not lie with your device; Apple's servers may be down. Visit Apple's System Status page to see if the AppStore is working properly. If there's a problem, all you can do is wait for Apple to fix it. Check your internet connection: Make sure you have a stable internet connection as the "Unable to connect to AppStore" issue can sometimes be attributed to a poor connection. Try switching between Wi-Fi and mobile data or resetting network settings (General > Reset > Reset Network Settings > Settings). Update your iOS version:

php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决 php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决 Jun 13, 2016 am 10:23 AM

php提交表单通过后,弹出的对话框怎样在当前页弹出php提交表单通过后,弹出的对话框怎样在当前页弹出而不是在空白页弹出?想实现这样的效果:而不是空白页弹出:------解决方案--------------------如果你的验证用PHP在后端,那么就用Ajax;仅供参考:HTML code

An article that understands this point and catches up with 70% of front-end people An article that understands this point and catches up with 70% of front-end people Sep 06, 2022 pm 05:03 PM

A colleague got stuck due to a bug pointed by this. Vue2’s this pointing problem caused an arrow function to be used, resulting in the inability to get the corresponding props. He didn't know it when I introduced it to him, and then I deliberately looked at the front-end communication group. So far, at least 70% of front-end programmers still don't understand it. Today I will share with you this link. If everything is wrong If you haven’t learned it yet, please give me a big mouth.

Is watch4pro better or gt? Is watch4pro better or gt? Sep 26, 2023 pm 02:45 PM

Watch4pro and gt each have different features and applicable scenarios. If you focus on comprehensive functions, high performance and stylish appearance, and are willing to bear a higher price, then Watch 4 Pro may be more suitable. If you don’t have high functional requirements and pay more attention to battery life and reasonable price, then the GT series may be more suitable. The final choice should be decided based on personal needs, budget and preferences. It is recommended to carefully consider your own needs before purchasing and refer to the reviews and comparisons of various products to make a more informed choice.

Java function access permission modifier public usage guide Java function access permission modifier public usage guide Apr 26, 2024 am 08:39 AM

The Java public access modifier allows functions to be accessed from anywhere and is used to declare public APIs and define tools and utilities that are shared across packages or classes. The specific usage is as follows: Syntax: public return value type function name (parameter list) {...} Scenario: functions that need to be accessed from anywhere, methods in public APIs, shared tools or utilities

See all articles