


PHP introductory tutorial: Analysis of how to operate MySQL with PHP
The example in this article describes how to operate MySQL with PHP. Share it with everyone for your reference, the details are as follows:
Set all UTF-8 encoding of Zend software
Set the UTF-8 encoding of a single project
Demo1.php
<?php header('Content-Type:text/html; charset=utf-8;'); //第一步,连接到 Mysql 服务器 3306 //第二步参数,服务器地址;第二个参数,服务器的用户名;第三个参数,服务器密码 //@ 如果出错了,不要出现警告或错误,直接忽略 //die 函数之前,先连接一下,报错流程 //echo (!!mysql_connect('localhost','root','123456')); // if(!$conn = @mysql_connect('localhost','root','123456')){ // echo '数据库连接失败,错误信息'.mysql_error(); // exit; // } // echo $conn; // echo '连接成功了,我才能显示!'; //常量参数 define('DB_HOST','localhost'); define('DB_USER','root'); define('DB_PWD','123456'); define('DB_NAME','school'); //第一步,连接数据库 //mysql_connect -- 打开一个到 MySQL 服务器的连接 $conn = @mysql_connect(DB_HOST,DB_USER,DB_PWD) or die('数据库连接失败,错误信息'.mysql_error()); //第二步,选择指定的数据库,设置字符集 //mysql_select_db -- 选择 MySQL 数据库 mysql_select_db(DB_NAME) or die ('数据库错误,错误信息:'.mysql_error()); mysql_query('SET NAMES UTF8')or die('字符集设置错误,错误信息'.mysql_error()); //第三步,从这个数据库里选一张表(grade),然后把这个表的数据库提出(获取记录集) $query = "SELECT * FROM grade"; //mysql_query -- 发送一条 MySQL 查询 $result = mysql_query($query) or die ('SQL错误:'.mysql_error()); //$result 就是记录集 //第四步,将记录集里的数据显示出来 print_r(mysql_fetch_array($result,MYSQL_NUM));//按照数字下标来显示 //print_r(mysql_fetch_array($result,MYSQL_ASSOC)); //按照字符串下标来显示 print_r(mysql_fetch_array($result,MYSQL_NUM)); print_r(mysql_fetch_array($result,MYSQL_NUM)); //第五步,释放记录集资源 //mysql_free_result -- 释放结果内存 mysql_free_result($result); //最后一步:关闭数据库 //mysql_close -- 关闭 MySQL 连接 echo mysql_close(); ?>
config. php
<?php header('Content-Type:text/html; charset=utf-8;'); //常量参数 define('DB_HOST','localhost'); define('DB_USER','root'); define('DB_PWD','123456'); define('DB_NAME','school'); //第一步,连接MYSQL 服务器 $conn = @mysql_connect(DB_HOST,DB_USER,DB_PWD) or die('数据库连接失败,错误信息'.mysql_error()); //第二步,选择指定的数据库,设置字符集 mysql_select_db(DB_NAME) or die ('数据库错误,错误信息:'.mysql_error()); mysql_query('SET NAMES UTF8')or die('字符集设置错误,错误信息'.mysql_error()); ?>
Demo2.php
<?php require 'config.php'; //新增数据 // $query = "INSERT INTO grade ( // name, // email, // point, // regdate) // VALUES ( // '景临境', // 'jly@163.com', // '78', // NOW() // )"; //$query = "INSERT INTO grade (name,email,point,regdate) VALUES ('与共','abc@163.com','78',NOW() )"; //mysql_query($query) or die('新增错误:'.mysql_error()); //修改数据 // $query = 'UPDATE grade SET point=66 WHERE id = 7'; // @mysql_query($query) or die('修改失败:'.mysql_error()); //删除数据 // $query = 'DELETE FROM grade WHERE id= 4'; // @mysql_query($query) or die('删除失败:'.mysql_error()); //显示数据 $query = 'SELECT id,name,email FROM grade '; $result = mysql_query($query) or die('SQL 语句有误:'.mysql_error()); // $row = mysql_fetch_array($result); // echo $row[2]; // $row = mysql_fetch_array($result); // echo $row[2]; //把结果集转换成数组赋给 $row ,如果有数据,就为真 while (!!$row = mysql_fetch_array($result)){ echo $row['id'].'----'.$row['name'].'-----'.$row['email']; echo '<br/>'; } mysql_close(); ?>
Demo3.php
<?php require 'config.php'; //显示数据 $query = 'SELECT id,email,name FROM grade '; $result = mysql_query($query) or die('SQL 语句有误:'.mysql_error()); // print_r(mysql_fetch_array($result)); // print_r(mysql_fetch_array($result,MYSQL_ASSOC)); // print_r(mysql_fetch_row($result)); // print_r(mysql_fetch_assoc($result)); // while (!!$row = mysql_fetch_array($result)){ // echo $row['id'].'----'.$row['name'].'-----'.$row['email']; // //print_r(mysql_fetch_lengths($result)); // echo mb_strlen($row['name'],'utf-8'); // echo '<br/>'; // } //echo mysql_field_name($result,2); //name //echo mysql_num_fields($result); //3 for($i=0; $i<mysql_num_fields($result);$i++){ echo mysql_field_name($result,$i); //id----email----name---- echo '----'; } echo '<br/>'; echo mysql_num_rows($result); //求出多少条数据 echo '<br/>'; echo mysql_get_client_info();//取得 MySQL 客户端信息 //5.0.51a echo '<br/>'; echo mysql_get_host_info();//取得 MySQL 主机信息 //localhost via TCP/IP echo '<br/>'; echo mysql_get_proto_info();//取得 MySQL 协议信息 //10 echo '<br/>'; echo mysql_get_server_info();//取得 MySQL 服务器信息 //5.0.51b-community-nt-log mysql_close(); ?>
For more PHP introductory tutorials and PHP operation MySQL method analysis related articles, please pay attention to 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



Alipay PHP...

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.
