Home > Backend Development > PHP Tutorial > PHP database operation and database connection tutorial

PHP database operation and database connection tutorial

黄舟
Release: 2023-03-15 07:56:02
Original
3370 people have browsed it

Whether it is in our daily work project development or in our own study, an indispensable part is the connection and operation of the database. I believe that everyone is already familiar with the steps to connect to the database. Connect to the database server, Select a database, execute SQL statements, and more.

First we need to download the php database operation encapsulation class used in this tutorial: //m.sbmmt.com/xiazai/leiku/826

When we connect to the database at work, we usually use the PHP extension mysqli to connect to the database and operate the database. Today we are talking about the PHP operation mysql database encapsulation class, using It is PDO to connect to the database!

First we create a database: abcd

PHP database operation and database connection tutorial

After creating the database, click on the database to create a data table: `t_table`,

PHP database operation and database connection tutorial

After creating the database, then instantiate the class:

<?php
include_once "mmysql.php";
$configArr = array(&#39;host&#39;=>&#39;localhost&#39;,&#39;port&#39;=>&#39;3306&#39;,&#39;user&#39;=>&#39;root&#39;,&#39;passwd&#39;=>&#39;root&#39;,&#39;dbname&#39;=>&#39;abcd&#39;);//这是连接数据库的操作
$mysql = new MMysql($configArr);
//插入的数值
$data = array(
    &#39;sid&#39;=>104,
    &#39;aa&#39;=>123456,
    &#39;bbc&#39;=>&#39;aaaaaaaaaaaaaa&#39;,
);
$arr = $mysql->insert(&#39;t_table&#39;,$data);//插入执行

//判断执行是否成功
if($arr){
    echo "插入成功";
}else{
    echo "插入失败";
}

//查询
$res = $mysql->field(array(&#39;sid&#39;,&#39;aa&#39;,&#39;bbc&#39;))
    ->order(array(&#39;sid&#39;=>&#39;desc&#39;,&#39;aa&#39;=>&#39;asc&#39;))
    ->where(array(&#39;sid&#39;=>"101",&#39;aa&#39;=>array(&#39;123455&#39;,&#39;>&#39;,&#39;or&#39;)))
    ->limit(1,2)
    ->select(&#39;t_table&#39;);
$res = $mysql->field(&#39;sid,aa,bbc&#39;)
    ->order(&#39;sid desc,aa asc&#39;)
    ->where(&#39;sid=101 or aa>123455&#39;)
    ->limit(1,2)
    ->select(&#39;t_table&#39;);

//获取最后执行的sql语句
$sql = $mysql->getLastSql();

//直接执行sql语句
$sql = "show tables";
$res = $mysql->doSql($sql);


//事务
$mysql->startTrans();
$mysql->where(array(&#39;sid&#39;=>102))->update(&#39;t_table&#39;,array(&#39;aa&#39;=>666666));
$mysql->where(array(&#39;sid&#39;=>103))->update(&#39;t_table&#39;,array(&#39;bbc&#39;=>&#39;呵呵8888呵呵&#39;));
$mysql->where(array(&#39;sid&#39;=>104))->delete(&#39;t_table&#39;);
$mysql->commit();
Copy after login

Finally get the running result:

PHP database operation and database connection tutorial


The above is the detailed content of PHP database operation and database connection tutorial. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template