Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the usage of mysqli_select_db and mysqli_query functions in PHP

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces what is the use of mysqli_select_db and mysqli_query functions in PHP. The content is very detailed. Interested friends can use it for reference. I hope it will be helpful to you.

PHP can connect to the MySQL database through the mysqli_connect () function, where one of the parameters is the name of the corresponding database, which is optional and can be omitted. If you omit this parameter, you need to specify a default database later, and you can specify a default database in PHP through the mysqli_select_db () function. Then let's take a look at the usage of this function.

Mysqli_select_db () function

The syntax format of this function is also divided into two cases, one is object-oriented writing, and its syntax format is as follows:

Mysqli::select_db (string $dbname)

Where $dbname represents the specified database name, and the other is process-oriented writing, whose syntax format is as follows:

Mysqli_select_db (mysqli $link, string $dbname)

Among them, it should be noted that:

$dbname is still represented as the specified database name

$link is represented as a database connection returned through the mysqli_connect () function.

If the function executes successfully, the result is true, the function fails, and the result is flase.

Next, let's take a look at how to select a database through the mysqli_select_db () function. The example is as follows:

In the above example, a database named "test" is selected through the mysqli_select_db () function, and the output is as follows:

The object-oriented method is used in the above example. Let's take a look at what the process-oriented method looks like. The example is as follows:

In the above example, there is not much difference between the two write methods, and the output is the same. So we specify a default database through the mysqli_select_db () function. So let's take a look at the mysqli_query () function. What does it do and how is it used?

Mysqli_query () function

In our above example, we have completed the specified selection of a database, and then we can query, change, and delete the data tables in the selected database. To do this, you need to use the mysqli_query () function, which has the following syntax format:

Mysqli::query (string $query [, int $resultmode = MYSQLI_STORE_RESULT])

This is an object-oriented way of writing, and one thing to note is:

$query indicates the SQL statement to be executed

Resultmode is an optional parameter that modifies the behavior of the function.

The following is a process-oriented syntax format, with examples as follows:

Mysqli_query (mysqli $link, string $query [, int $resultmode = MYSQLI_STORE_RESULT])

Among them, it should be noted that:

$link represents the database connection returned by the mysqli_connect () function

$query indicates the SQL statement to be executed

Resultmode is an optional parameter that modifies the behavior of the function.

Next, let's take a look at the use of the mysqli_query () function through an example, as follows:

Before we begin, let's add some data to the data table for example

Mysql > select * from user

+-- +

| | id | name | age | sex | |

+-- +

| | 1 | one | 21 | male |

| | 2 | II | 22 | male |

| | 3 | 3 | 23 | female |

| | 4 | wanton | 24 | female |

+-- +

4 rows in set (0.00 sec)

First of all, let's take a look at object-oriented writing. The examples are as follows:

Output result:

The above is written in an object-oriented way, and the process-oriented way is as follows:

The output is the same as in the example above, so we have finished querying the database through the mysqli_query () function.

So much for sharing the usage of mysqli_select_db and mysqli_query functions in PHP. I hope the above can help you and learn more. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report