Home > Backend Development > PHP Tutorial > php link to mysql database

php link to mysql database

WBOY
Release: 2016-07-29 09:09:05
Original
1098 people have browsed it

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.

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template