PHP MySQL database method of operation - how to choose the database file

PHP development in our daily work, when we get data from the database, the database connection after PHP, the next step is we have to choose the database file, it is necessary to use a function, mysql_select_db () function to select the database! Which is good marble Measuring Tool

Before selecting a database, we first have to do is connect to the database PHP, and today we are mainly talking about mysql_select_db () function!

the mysql_select_db () function has the following syntax:

1

mysql_select_db(string 数据库名[,resource link_identifier])

 

or:

1

mysql_query("use 数据库名"[,resource link_identifier])

The following example uses the mysql_select_db () function to connect the database, the database is php_cn, specific examples code is as follows:

1

2

3

4

5

6

7

8

<?php

header("Content-Type:text/html; charset=utf-8");

$link = mysql_connect("localhost","root","root")or die("不能连接到数据库服务器!".mysql_error()); //连接MySQL 服务器

$db_selected = mysql_select_db("php_cn",$link);                  //选择数据库php_cn

if($db_selected){

    echo "选择数据库成功";

}

?>

Operating results as follows:

 

Here supplement, the above code $ db_selected = mysql_select_db ( "php_cn", $ link); can alternatively use the following code:

1

$db_selected = mysql_query("use php_cn",$link);                  //选择数据库php_cn

mysql_query () function is a special function query command, all of the SQL statements executed by it, and returns the result set.

Guess you like

Origin www.cnblogs.com/furuihua/p/12172307.html