Learning PHP--Two ways to connect to MySQL
Record two ways for PHP to connect to MySQL.
First mock the data and execute sql.
<span>/*</span><span>创建数据库</span><span>*/</span> <span>CREATE</span> <span>DATABASE</span> <span>IF</span> <span>NOT</span> <span>EXISTS</span><span> `test`; </span><span>/*</span><span>选择数据库</span><span>*/</span> <span>USE</span><span> `test`; </span><span>/*</span><span>创建表</span><span>*/</span> <span>CREATE</span> <span>TABLE</span> <span>IF</span> <span>NOT</span> <span>EXISTS</span> `<span>user</span><span>` ( name </span><span>varchar</span>(<span>50</span><span>), age </span><span>int</span><span> ); </span><span>/*</span><span>插入测试数据</span><span>*/</span> <span>INSERT</span> <span>INTO</span> `<span>user</span>` (name, age) <span>VALUES</span>(<span>'</span><span>harry</span><span>'</span>, <span>20</span>), (<span>'</span><span>tony</span><span>'</span>, <span>23</span>), (<span>'</span><span>harry</span><span>'</span>, <span>24</span>);
The first is to use PHP’s native method to connect to the database. The code is as follows:
<?<span>php </span><span>$host</span> = 'localhost'<span>; </span><span>$database</span> = 'test'<span>; </span><span>$username</span> = 'root'<span>; </span><span>$password</span> = 'root'<span>; </span><span>$selectName</span> = 'harry';<span>//</span><span>要查找的用户名,一般是用户输入的信息</span> <span>$connection</span> = <span>mysql_connect</span>(<span>$host</span>, <span>$username</span>, <span>$password</span>);<span>//</span><span>连接到数据库</span> <span>mysql_query</span>("set names 'utf8'");<span>//</span><span>编码转化</span> <span>if</span> (!<span>$connection</span><span>) { </span><span>die</span>("could not connect to the database.\n" . <span>mysql_error</span>());<span>//</span><span>诊断连接错误</span> <span>} </span><span>$selectedDb</span> = <span>mysql_select_db</span>(<span>$database</span>);<span>//</span><span>选择数据库</span> <span>if</span> (!<span>$selectedDb</span><span>) { </span><span>die</span>("could not to the database\n" . <span>mysql_error</span><span>()); } </span><span>$selectName</span> = <span>mysql_real_escape_string</span>(<span>$selectName</span>);<span>//</span><span>防止SQL注入</span> <span>$query</span> = "select * from user where name = '<span>$selectName</span>'";<span>//</span><span>构建查询语句</span> <span>$result</span> = <span>mysql_query</span>(<span>$query</span>);<span>//</span><span>执行查询</span> <span>if</span> (!<span>$result</span><span>) { </span><span>die</span>("could not to the database\n" . <span>mysql_error</span><span>()); } </span><span>while</span> (<span>$row</span> = <span>mysql_fetch_row</span>(<span>$result</span><span>)) { </span><span>//</span><span>取出结果并显示</span> <span>$name</span> = <span>$row</span>[0<span>]; </span><span>$age</span> = <span>$row</span>[1<span>]; </span><span>echo</span> "Name: <span>$name</span> "<span>; </span><span>echo</span> "Age: <span>$age</span> "<span>; </span><span>echo</span> "\n"<span>; }</span>
The operating structure is as follows:
Name: harry Age: 20<span> Name</span>: tony Age: 23
The second method is to use PDO to connect to the database. The code is as follows:
<?<span>php </span><span>$host</span> = 'localhost'<span>; </span><span>$database</span> = 'test'<span>; </span><span>$username</span> = 'root'<span>; </span><span>$password</span> = 'root'<span>; </span><span>$selectName</span> = 'harry';<span>//</span><span>要查找的用户名,一般是用户输入的信息</span> <span>$pdo</span> = <span>new</span> PDO("mysql:host=<span>$host</span>;dbname=<span>$database</span>", <span>$username</span>, <span>$password</span>);<span>//</span><span>创建一个pdo对象</span> <span>$pdo</span>-><span>exec</span>("set names 'utf8'"<span>); </span><span>$sql</span> = "select * from user where name = ?"<span>; </span><span>$stmt</span> = <span>$pdo</span>->prepare(<span>$sql</span><span>); </span><span>$rs</span> = <span>$stmt</span>->execute(<span>array</span>(<span>$selectName</span><span>)); </span><span>if</span> (<span>$rs</span><span>) { </span><span>//</span><span> PDO::FETCH_ASSOC 关联数组形式 // PDO::FETCH_NUM 数字索引数组形式</span> <span>while</span> (<span>$row</span> = <span>$stmt</span>->fetch(PDO::<span>FETCH_ASSOC)) { </span><span>$name</span> = <span>$row</span>['name'<span>]; </span><span>$age</span> = <span>$row</span>['age'<span>]; </span><span>echo</span> "Name: <span>$name</span> "<span>; </span><span>echo</span> "Age: <span>$age</span> "<span>; </span><span>echo</span> "\n"<span>; } } </span><span>$pdo</span> = <span>null</span>;<span>//</span><span>关闭连接</span>
The result is the same as the first one.
The above introduces the learning of PHP - two ways to connect to MySQL, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

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



The main reasons why you cannot log in to MySQL as root are permission problems, configuration file errors, password inconsistent, socket file problems, or firewall interception. The solution includes: check whether the bind-address parameter in the configuration file is configured correctly. Check whether the root user permissions have been modified or deleted and reset. Verify that the password is accurate, including case and special characters. Check socket file permission settings and paths. Check that the firewall blocks connections to the MySQL server.

Data Integration Simplification: AmazonRDSMySQL and Redshift's zero ETL integration Efficient data integration is at the heart of a data-driven organization. Traditional ETL (extract, convert, load) processes are complex and time-consuming, especially when integrating databases (such as AmazonRDSMySQL) with data warehouses (such as Redshift). However, AWS provides zero ETL integration solutions that have completely changed this situation, providing a simplified, near-real-time solution for data migration from RDSMySQL to Redshift. This article will dive into RDSMySQL zero ETL integration with Redshift, explaining how it works and the advantages it brings to data engineers and developers.

Yes, MySQL can be installed on Windows 7, and although Microsoft has stopped supporting Windows 7, MySQL is still compatible with it. However, the following points should be noted during the installation process: Download the MySQL installer for Windows. Select the appropriate version of MySQL (community or enterprise). Select the appropriate installation directory and character set during the installation process. Set the root user password and keep it properly. Connect to the database for testing. Note the compatibility and security issues on Windows 7, and it is recommended to upgrade to a supported operating system.

When MySQL modifys table structure, metadata locks are usually used, which may cause the table to be locked. To reduce the impact of locks, the following measures can be taken: 1. Keep tables available with online DDL; 2. Perform complex modifications in batches; 3. Operate during small or off-peak periods; 4. Use PT-OSC tools to achieve finer control.

1. Use the correct index to speed up data retrieval by reducing the amount of data scanned select*frommployeeswherelast_name='smith'; if you look up a column of a table multiple times, create an index for that column. If you or your app needs data from multiple columns according to the criteria, create a composite index 2. Avoid select * only those required columns, if you select all unwanted columns, this will only consume more server memory and cause the server to slow down at high load or frequency times For example, your table contains columns such as created_at and updated_at and timestamps, and then avoid selecting * because they do not require inefficient query se

MySQL can handle multiple concurrent connections and use multi-threading/multi-processing to assign independent execution environments to each client request to ensure that they are not disturbed. However, the number of concurrent connections is affected by system resources, MySQL configuration, query performance, storage engine and network environment. Optimization requires consideration of many factors such as code level (writing efficient SQL), configuration level (adjusting max_connections), hardware level (improving server configuration).

MySQL has a free community version and a paid enterprise version. The community version can be used and modified for free, but the support is limited and is suitable for applications with low stability requirements and strong technical capabilities. The Enterprise Edition provides comprehensive commercial support for applications that require a stable, reliable, high-performance database and willing to pay for support. Factors considered when choosing a version include application criticality, budgeting, and technical skills. There is no perfect option, only the most suitable option, and you need to choose carefully according to the specific situation.

MySQL cannot run directly on Android, but it can be implemented indirectly by using the following methods: using the lightweight database SQLite, which is built on the Android system, does not require a separate server, and has a small resource usage, which is very suitable for mobile device applications. Remotely connect to the MySQL server and connect to the MySQL database on the remote server through the network for data reading and writing, but there are disadvantages such as strong network dependencies, security issues and server costs.
