Home Backend Development PHP Tutorial php link to mysql database

php link to mysql database

Jul 29, 2016 am 09:09 AM
fetch mysql nbsp quot

There are three ways to connect to the database in PHP. I just discovered that the mysql_query connection through mysql_connect has been abandoned, and now it is recommended to connect to the database through the "object-oriented method" and the "PDO method".

When I used the object-oriented method to connect, the query results could not be displayed in Chinese. Most of the solutions given on the Internet were for the old connection method, but I finally found a solution.

The general connection method is as follows:

<span><?php
</span><span><span>header</span><span>(</span><span>"Content-type: text/html; charset=utf-8"</span><span>);
</span><span>$mysql_url</span><span>=</span><span>"localhost"</span><span>;</span><span>//</span><span>数据库服务器的地址
</span><span>$mysql_username</span><span>=</span><span>""</span><span>;</span><span>//</span><span>数据库用户名的名称
</span><span>$mysql_password</span><span>=</span><span>""</span><span>;</span><span>//</span><span>连接数据库的密码
</span><span>$mysql_database</span><span>=</span><span>""</span><span>;</span><span>//</span><span>数据库的名字
</span><span>$db</span><span>=</span><span>new </span><span>mysqli(</span><span>$mysql_url</span><span>,</span><span>$mysql_username</span><span>,</span><span>$mysql_password</span><span>,</span><span>$mysql_database</span><span>);
</span><span>if</span><span>(</span><span>mysqli_connect_error</span><span>()){</span><span>//</span><span>数据库连接失败时提示
</span><span>echo </span><span>'Could not connect to database.'</span><span>;
</span><span>exit</span><span>;
</span><span>}
</span><span>mysqli_query</span><span>(</span><span>$db</span><span>,</span><span>'set names utf8'</span><span>);</span><span>//</span><span>给查询结果设置编码
</span><span>$result</span><span>=</span><span>$db</span><span>->query(</span><span>"SELECT * FROM yc_brand"</span><span>);</span><span>//</span><span>此处</span><span>sql</span><span>语句表面不能加单引号或双引号
</span><span>$all</span><span>=</span><span>$result</span><span>->fetch_all();</span><span>//</span><span>获取结果集中的所有数据</span><span>var_dump</span><span>(</span><span>$all</span><span>);
</span><span>?></span>
Copy after login

The query result is an object $result. Because it is an object, it cannot be printed and used directly.

We can call the method of the object to convert it into an array or other representation.

The more commonly used ones are fetch_row, fetch_array, fetch_all

1. fetch_row()

$row = $result->fetch_row()

obtained by this method $row is a one Dimensional array, you will only get one set of "records" each time, that is: if you find out there should be 10 sets of records, and it will only return you one set of records each time.

You need to use a while loop to load the one-dimensional array queried each time into a one-dimensional array to form a two-dimensional array (such as a two-dimensional array containing 10 sets of records).

The characteristics of this method are: the key names in the queried one-dimensional array default to numbers starting from 0

<span>$rows</span><span><span>= </span><span>array</span><span>();</span><span>//</span><span>建立一个数组用来装查询结果
</span><span>while</span><span>(</span><span>$row </span><span>= </span><span>$result</span><span>->fetch_row()){</span><span>//</span><span>只要能查到结果就执行
</span><span>$rows</span><span>[] = </span><span>$row</span><span>;</span><span>//</span><span>将每次查的结果装到之前定义的数组
</span><span>}
</span><span>var_dump</span><span>(</span><span>$rows</span><span>);
</span></span>
Copy after login

2. fetch_array()

$row = $result-> fetch_array()

This method is generally the same as fetch_row. The difference between them is that each column in the one-dimensional array found by fetch_array() has two key values. The column names of the original table will automatically become Each key value name will also have a key value name that is automatically sorted starting from 0

3. fetch_all()

$rows = $result ->fetch_all();

This method is obtained The $rows that comes out is a two-dimensional array. In fact, it is "equivalent to the two-dimensional array $rows that the fetch_row method has cycled through to save the one-dimensional array" and can be printed directly.

The above introduces the PHP link to the MySQL database, including aspects of the content. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to open phpmyadmin How to open phpmyadmin Apr 10, 2025 pm 10:51 PM

You can open phpMyAdmin through the following steps: 1. Log in to the website control panel; 2. Find and click the phpMyAdmin icon; 3. Enter MySQL credentials; 4. Click "Login".

MySQL: An Introduction to the World's Most Popular Database MySQL: An Introduction to the World's Most Popular Database Apr 12, 2025 am 12:18 AM

MySQL is an open source relational database management system, mainly used to store and retrieve data quickly and reliably. Its working principle includes client requests, query resolution, execution of queries and return results. Examples of usage include creating tables, inserting and querying data, and advanced features such as JOIN operations. Common errors involve SQL syntax, data types, and permissions, and optimization suggestions include the use of indexes, optimized queries, and partitioning of tables.

How to use single threaded redis How to use single threaded redis Apr 10, 2025 pm 07:12 PM

Redis uses a single threaded architecture to provide high performance, simplicity, and consistency. It utilizes I/O multiplexing, event loops, non-blocking I/O, and shared memory to improve concurrency, but with limitations of concurrency limitations, single point of failure, and unsuitable for write-intensive workloads.

MySQL's Place: Databases and Programming MySQL's Place: Databases and Programming Apr 13, 2025 am 12:18 AM

MySQL's position in databases and programming is very important. It is an open source relational database management system that is widely used in various application scenarios. 1) MySQL provides efficient data storage, organization and retrieval functions, supporting Web, mobile and enterprise-level systems. 2) It uses a client-server architecture, supports multiple storage engines and index optimization. 3) Basic usages include creating tables and inserting data, and advanced usages involve multi-table JOINs and complex queries. 4) Frequently asked questions such as SQL syntax errors and performance issues can be debugged through the EXPLAIN command and slow query log. 5) Performance optimization methods include rational use of indexes, optimized query and use of caches. Best practices include using transactions and PreparedStatemen

Why Use MySQL? Benefits and Advantages Why Use MySQL? Benefits and Advantages Apr 12, 2025 am 12:17 AM

MySQL is chosen for its performance, reliability, ease of use, and community support. 1.MySQL provides efficient data storage and retrieval functions, supporting multiple data types and advanced query operations. 2. Adopt client-server architecture and multiple storage engines to support transaction and query optimization. 3. Easy to use, supports a variety of operating systems and programming languages. 4. Have strong community support and provide rich resources and solutions.

How to connect to the database of apache How to connect to the database of apache Apr 13, 2025 pm 01:03 PM

Apache connects to a database requires the following steps: Install the database driver. Configure the web.xml file to create a connection pool. Create a JDBC data source and specify the connection settings. Use the JDBC API to access the database from Java code, including getting connections, creating statements, binding parameters, executing queries or updates, and processing results.

How to start mysql by docker How to start mysql by docker Apr 15, 2025 pm 12:09 PM

The process of starting MySQL in Docker consists of the following steps: Pull the MySQL image to create and start the container, set the root user password, and map the port verification connection Create the database and the user grants all permissions to the database

Centos install mysql Centos install mysql Apr 14, 2025 pm 08:09 PM

Installing MySQL on CentOS involves the following steps: Adding the appropriate MySQL yum source. Execute the yum install mysql-server command to install the MySQL server. Use the mysql_secure_installation command to make security settings, such as setting the root user password. Customize the MySQL configuration file as needed. Tune MySQL parameters and optimize databases for performance.

See all articles