Description: PHP connects to MySQL server
Syntax: resource $link = mysql_connect($hostname, $username, $password)
Parameters:
$hostname: It is the domain name or IP address of the MySQL server, and you can also add the port number (3306). For example: localhost:3306
#$username: is the username of the MySQL server. For example: root
#$password: is the user password of the MySQL server.
Return value: If the execution is successful, a resource identifier is returned. If the execution fails, return FALSE If the
character to block this system error and improve security. <?php
ini_set(display_errors, 1); //设置错误为:显示
//0.数据库配置信息
$db_host = 'loca1lhost';
$db_user = 'root';
$db_pwd = 'yeoman';
//1.PHP连接MySQL服务器
$link = @mysql_connect($db_host, $db_user, $db_pwd); //@用于屏蔽系统错误
//如果连接失败,则输出错误信息,并中止程序运行
if(!$link){
echo "PHP连接MySQL失败!".mysql_error();
exit(); //中止程序继续向下运行
}
?>
Description: Returns the text error message when the last MySQL execution failed.
open active connection will be used.
Return value: If the database selection is successful,
will be returned. If database selection fails, FALSE is returned.
Operation
Warning: mysql_connect() [<a href='function.mysql-connect'>function.mysql-connect</a>]: Unknown MySQL server host 'local1host' (0) in /Applications/MAMP/htdocs/php_test/php_mysql.php on line 17
<?php //0.数据库配置信息 $db_name = 'yeoman92'; //2.选择当前数据库 if(!mysql_select_db($db_name, $link)){ echo "选择数据库{$db_name}失败!".mysql_error(); exit(); } ?>
##mysql_query()
Add records: $query = "INSERT INTO news(title,content) VALUES('title', 'content')"
Delete records: $query = “DELETE FROM news WHERE id=10”
Modify record: $query = “UPDATE news SET title = ‘New title’, content = ‘New content’ WHERE id= 80"
$query:指要发送的SQL语句。
$link:可选项。指上一次打开的活动连接。
返回值:
结果集也是一个资源标识符,是到MySQL数据的一个引用地址。
如果执行SELECT、SHOW、DESCRIBE命令,成功时返回结果集,失败时返回FALSE。
如果执行其它的SQL语句,执行成功返回TRUE,执行失败返回FALSE。
举例:
$result = mysql_query(“SELECT * FROM news WHERE id<100”)
$result = mysql_query(“DELETE * FROM news WHERE id=100”)
//4.执行SQL查询:向MySQL服务器发出查询的请求$ sql = 'SELECT * FROM 007_news WHERE id<100'; $result = mysql_query($sql, $link);
描述:从结果集中取得一行作为枚举数组。
语法:array mysql_fetch_row ( resource $result )
参数:$result是结果集变量。
返回值:返回一个枚举数组,也就是从0开始的正整数下标。这里的下标,是与表的字段下标是对应的。
举例:$arr = mysql_fetch_row($result)
//5.取出一条记录作为枚举数组返回 $arr = mysql_fetch_row($result);echo "<pre class="brush:php;toolbar:false">"; print_r($arr);echo "";
Array( [0] => 17 [1] => 1 [2] => cctv中学生信息化教学实验基地共建工程在京启动 [3] => admin [4] => 中小学课外辅导网 [5] => [6] => [7] => 50 [8] => [9] => 52 [10] => 1339340691)
描述:从结果集中取出一行,作为混合数组返回。
语法:array mysql_fetch_array ( resource $result [, int $result_type ] )
参数:
MYSQL_BOTH:默认的。也就是两种下标都存在。
MYSQL_ASSOC:只有字符下标的数组。相当于mysql_fetch_assoc()的功能。
MYSQL_NUM:只有整数下标的数组。相当于mysql_fetch_row()的功能。
以上三个参数是常量,系统常量必须全大写。
$result:是指结果集变量。
$result_type:是指返回的数组的类型。取值:MYSQL_BOTH、MYSQL_ASSOC、MYSQL_NUM
返回值:返回一个数组,至于是什么数组,取决于第二个参数。
举例:
$arr = mysql_fetch_array($result) //混合数组
$arr = mysql_fetch_array($result , MYSQL_ASSOC ) //关联数组
$arr = mysql_fetch_array($result , MYSQL_NUM) //枚举数组
描述:从结果集中取一行,以关联数组返回。
语法:array mysql_fetch_assoc(resource $result)
举例:$arr = mysql_fetch_assoc($result)
<?php ini_set(display_errors, 1); //设置错误为:显示 //0.数据库配置信息 $db_host = 'localhost'; $db_user = 'root'; $db_pwd = 'yeoman'; $db_name = 'yeoman92'; //1.PHP连接MySQL服务器 $link = @mysql_connect($db_host, $db_user, $db_pwd); //@用于屏蔽系统错误 //如果连接失败,则输出错误信息,并中止程序运行 if(!$link){ echo "PHP连接MySQL失败!".mysql_error(); exit(); //中止程序继续向下运行 } //2.选择当前数据库 if(!mysql_select_db($db_name, $link)){ echo "选择数据库{$db_name}失败!".mysql_error(); exit(); } //3.设置MySQL返回的数据字符集 mysql_query("set names utf8"); //4.执行SQL查询:向MySQL服务器发出查询的请求 $sql = 'SELECT * FROM 007_news WHERE id<100'; $result = mysql_query($sql, $link); //5.取出一条记录作为枚举数组返回 $arr = mysql_fetch_row($result); echo "<pre class="brush:php;toolbar:false">"; print_r($arr); echo ""; ?>
The above is the detailed content of Detailed explanation of operating MySQL database with PHP (1). For more information, please follow other related articles on the PHP Chinese website!