How to connect to database with PHP
Process-oriented implementation of database connection
<?php //一、面向过程的编码风格 //1.PHP与MySQL建立连接 /*语法mysqli_connect(host,username,passwd,dbname,port); mysqli_connect()方法具有返回值,返回值就是PHP与MySQL数据库建立连接的连接对象 *host - MySQL数据库所在的计算机的ip地址 * username - 登陆MySQL数据库的用户名称 * passwd - 登陆MySQL数据库的用户密码(如果密码为空则写 “” 站位) * dbname - 要操作的数据库的名称 * port - MySQL数据库所使用的端口号(一般默认为3306,不建议修改哦) */ $connect = mysqli_connect('127.0.0.1','root','','bigspinach','3306'); //2.PHP向MySQL数据库发送sql语句,并接收返回的结果 //2.1 编写sql语句 /* *增删改操作返回Boolean值 * 增:INSERT INTO 表名 VALUES (所有字段值) 注意:主键自增字段需写成 NULL INSERT INTO 表名 字段名=新字段值 WHERE 字段名=字段值 * 删:DELETE FROM 表名 DELETE FROM 表名 WHERE 字段名=字段值 * 改 :UPDATE 表名 SET 字段名=字段值 注意:这样修改会修改所有该字段的值 UPDATE 表名 SET 字段名=新字段值 WHERE 字段名=字段值 *查操作返回 结果集对象 (1)基本查询 SELECT * FROM 表名 SELECT 字段名1,字段名2,... FROM 表名 (2)条件查询 SELECT * FROM 表名 WHERE 字段值=字段名 (3)排序查询 SELECT * FROM 表名 ORDER BY 字段名 正序: SELECT * FROM 表名 ORDER BY 字段名 ASC 倒序:SELECT * FROM 表名 ORDER BY 字段名 DESC (4)模糊查询 SELECT * FROM 表名 WHERE 字段名 LIKE "字符串%%" 字符串%字符串 (5)LIMIT(MySQL方言) SELECT * FROM 表名 LIMIT [位置偏移量,]行数; SELECT * FROM 表名 LIMIT 0,5; 查询第一行数据开始,显示5条 //解析查询结果(mysqli_result对象) mysqli_result对象 * 属性 * $field_count 字段数量 * $num_rows 多少条数据记录 * 方法 *(1) mysqli_fetch_array($result,[result]); *$result - 结果集对象 * result - 解析为数组的返回数组的方式 * a.关联数组mysqli_assoc; * b索引数组mysqli_num; * c.两种数组都返回mysqli_both(默认值)) 方法的具体使用 $arr=new array(); while($row=mysqli_fetch_array($result,num)){ array_push($arr,$row); *$arr:要压入的数组 * $row:循环得到的每一条数据记录 } *(2) mysqli_fetch_assoc($result) 该方法返回索引数组 方法的具体使用 $arr=new array(); while($row=mysqli_fetch_assoc($result)){ array_push($arr,$row); } */ $sql = "DELETE FROM liukai ";//删除名为 liukai 的数据表 //2.2解决中文乱码问题--固定套路 mysqli_query($connect连接对象,'SET NAMES UTF8'); mysqli_query($connect,'SET NAMES UTF8'); //2.3将sql语句发送给MySQL数据库,并接收其返回的结果 $result = mysqli_query($connect,$sql); //3.PHP与MySQL数据库关闭连接 mysqli_close($connect); ?>
Simplified:
<?php //1.建立连接 $connect=mysqli_connect('localhost','root','','bigspinach','3306'); //2.定义sql语句 $sql='select * from liukai'; mysqli_query($connect,'set names utf8'); //3.发送SQL语句 $result=mysqli_query($connect,$sql); $arr=array();//定义空数组 while($row =mysqli_fetch_array($result)){ //var_dump($row); //array_push(要存入的数组,要存的值) array_push($arr,$row); } var_dump($arr); //4.关闭连接 mysqli_close($connect); ?>
Oriented to Implementing database connection in object mode
<?php //面向对象的编码风格 //1.new一个 mysqli(host,username,passwd,dbname,port)对象 ====等同于建立连接 //2.定义sql语句 //3.调用mysqli 对象 的 query($sql)方法并得到其返回结果值 //4.调用mysqli 对象 的 close()方法关闭连接 $mysqli = new mysqli('127.0.0.1','root','','bigspinach','3306'); $sql = "select * from where 姓名 = liukai "; $mysqli -> query('set names utf8'); $mysqli -> query($sql); $mysqli -> close(); ?>
Recommended tutorial:PHP video tutorial
The above is the detailed content of How to connect to database with PHP. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

In this chapter, we are going to learn the following topics related to routing ?

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

Validator can be created by adding the following two lines in the controller.
