PHP MYSQL进行数据库查询的方法:首先连接数据库服务器;然后设置字符集,使页面的编码与数据库的编码一致;接着编写数据库查询命令;最后执行查询命令即可。
PHP MYSQL怎么进行数据库查询?
PHP MYSQL进行数据库查询
一、基本代码
mysql_connect():用来建立和MYSQL数据库连接的,共有5个参数,通常情况下只用前3个参数。3个参数分别是MySQL服务器地址、用户名和密码。
mysql_select_db():用来指定要操作的数据库。如果要操作的数据库还没有创建,则要先创建数据库,接着再创建数据库中的表。
mysql_query():查询指令的专用函数,所有的SQL语句都通过它执行,并返回结果集。
mysql_fetch_row():从结果集中取一行作为枚举数据,从和指定的结果标识关联的结果集中取得一行数据并作为数组返回。
mysql_fetch_array():从结果集中取得一行作为关联数组,或数字数组,或二者兼有,除了将数据以数字索引方式储存在数组中之外,还可以将数据作为关联索引储存,用字段名作为键名。
mysql_fetch_object():从结果集中取得一行作为对象,并将字段名字做为属性。
mysql_fetch_assoc():从结果集中取得一行作为关联数组,也就是说这个函数不能像mysql_fetch_row那样用索引来取值,只能用字段名字来取。
mysql_num_rows():获取由select语句查询到的结果集中行的数目
二、示例
<?php /* 连接数据库服务器,用or die的目的为了即使连接错误,系统不会继续执行,而是返回错误*/ $link=mysql_connect("localhost","root","12345678") or die("数据库连接失败"); /* 连接数据库*/ mysql_select_db("phptest",$link); /*设置字符集,使得页面的编码与数据库的编码一致。如果不一致将出现中文乱码*/ mysql_query("set names utf8"); /*定义数据库命令查询*/ $q="select*from tb_user"; /*执行数据库查询*/ $result=mysql_query($q); /*执行数据库记录*/ while($row=mysql_fetch_assoc($result)){ echo "<tr><td>".$row["id"]."</td><td>".$row["username"]."</td><td>".$row["password"]."</td></tr>"; /*显示数据结果*/ } ?>
三、实例
PHP页面querytest.php
无标题文档
<?php /* 连接数据库服务器,用or die的目的为了即使连接错误,系统不会继续执行,而是返回错误*/ $link=mysql_connect("localhost","root","12345678") or die("数据库连接失败"); /* 连接数据库*/ mysql_select_db("phptest",$link); /*设置字符集,使得页面的编码与数据库的编码一致。如果不一致将出现中文乱码*/ mysql_query("set names utf8"); /*定义数据库命令查询*/ $q="select*from tb_user"; /*执行数据库查询*/ $result=mysql_query($q); /*执行数据库记录*/ while($row=mysql_fetch_assoc($result)){ echo "<tr><td>".$row["id"]."</td><td>".$row["username"]."</td><td>".$row["password"]."</td></tr>"; /*显示数据结果*/ } ?> 序号 用户名 密码
更多相关技术知识,请访问PHP中文网!

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python are both high-level programming languages that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

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.


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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version
Visual web development tools

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

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version
Useful JavaScript development tools