


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!

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.

In PHP, use the clone keyword to create a copy of the object and customize the cloning behavior through the \_\_clone magic method. 1. Use the clone keyword to make a shallow copy, cloning the object's properties but not the object's properties. 2. The \_\_clone method can deeply copy nested objects to avoid shallow copying problems. 3. Pay attention to avoid circular references and performance problems in cloning, and optimize cloning operations to improve efficiency.

PHP is suitable for web development and content management systems, and Python is suitable for data science, machine learning and automation scripts. 1.PHP performs well in building fast and scalable websites and applications and is commonly used in CMS such as WordPress. 2. Python has performed outstandingly in the fields of data science and machine learning, with rich libraries such as NumPy and TensorFlow.


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

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

WebStorm Mac version
Useful JavaScript development tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Atom editor mac version download
The most popular open source editor