Home  >  Article  >  php教程  >  十天学会php之第四天

十天学会php之第四天

WBOY
WBOYOriginal
2016-06-13 12:43:291252browse

 

学习目的:学会连接数据库

PHP简直就是一个函数库,丰富的函数使PHP的某些地方相当简单。建议大家down一本PHP的函数手册,总用的到。

我这里就简单说一下连接MYSQL数据库。

1、mysql_connect

打开 MySQL 服务器连接。
语法: int mysql_connect(string [hostname] [:port], string [username], string [password]); 返回值: 整数

本函数建立与 MySQL 服务器的连接。其中所有的参数都可省略。当使用本函数却不加任何参数时,参数 hostname 的默认值为 localhost、参数 username 的默认值为 PHP 执行行程的拥有者、参数 password 则为空字符串 (即没有密码)。而参数 hostname 后面可以加冒号与端口号,代表使用哪个端口与 MySQL 连接。当然在使用数据库时,早点使用 mysql_close() 将连接关掉可以节省资源。

2、 mysql_select_db

选择一个数据库。
语法: int mysql_select_db(string database_name, int [link_identifier]); 返回值: 整数

本函数选择 MySQL 服务器中的数据库以供之后的资料查询作业 (query) 处理。成功返回 true,失败则返回 false。

最简单的例子就是:
$conn=mysql_connect ("127.0.0.1", "", "");
mysql_select_db("shop");
连接机MY SQL数据库,打开SHOP数据库。在实际应用中应当加强点错误判断。

今天就说到这里,明天再说一下数据库的读取。

Statement:
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