PHP连接MySQL 及 MySQL基本函数

 1 <?php
 2 
 3 $db_config = array(
 4     'host' => 'localhost',
 5     'port' => 3306,
 6     'user' => 'root',
 7     'pwd' => '123456',
 8     'dbname' => 'ys_db'
 9 );
10 
11 //第一步:连接mysql
12 $conn = mysql_connect($db_config['host'], $db_config['user'], $db_config['pwd']) or die('unable to connect!');
13 
14  mysql_query("set names 'gb2312'");
15 //第二步:连接数据库
16 mysql_select_db($db_config['dbname']);
17 
18 //第三步:sql语句
19 $sql = 'SELECT * FROM tb_friend_link ORDER BY Fsort';
20 
21 //第四步:执行sql
22 $result = mysql_query($sql) or die('error in query : ' . $sql . ' , ' . mysql_error());
23 
24 $totle = mysql_num_rows($result);
25 if ($totle > 0) {
26     $rows = array();
27     while ($row = mysql_fetch_assoc($result)) {
28         $rows[] = $row;
29     }
30     print_r($rows);
31 } else {
32     echo '没有数据';
33 }
34 
35 //第五步:释放数据
36 mysql_free_result($result);
37 
38 //第六步:关闭连接
39 mysql_close($conn);

 

 1 /**
 2  *  常用函数讲解及测试 
 3  */
 4 mysql_connect();  //连接数据库
 5 mysql_select_db();  //选择数据库
 6 mysql_query();   //执行sql语句
 7 mysql_free_result();  //释放数据
 8 mysql_close();   //关闭连接
 9 
10 mysql_errno();  //返回上一个 MySQL 操作中的错误信息的数字编码 
11 mysql_error();  //返回上一个 MySQL 操作产生的文本错误信息
12 
13 $result = mysql_query($sql);
14 mysql_num_rows($result);  //返回数据条数
15 
16 mysql_fetch_assoc($result);  //从结果集中取得一行作为关联数组,关联数组及下标为Fname,Fid
17 mysql_fetch_row($result);   //从结果集中取得一行作为枚举数组,枚举数组下标为数字
18 
19 mysql_insert_id();  // 取得上一步 INSERT 操作产生的 ID 
20 mysql_affected_rows();   //取得前一次 MySQL 操作所影响的记录行数
21 
22 /**
23  *  需要了解的函数
24  */
25 mysql_client_encoding($conn);   //返回字符集的名称
26 
27 mysql_get_client_info(); //取得 MySQL 客户端信息
28 mysql_get_host_info(); //取得 MySQL 主机信息
29 mysql_get_proto_info(); //取得 MySQL 协议信息
30 mysql_get_server_info(); //取得 MySQL 服务器信息
31 
32 $status = explode('  ', mysql_stat($conn));  //取得当前系统状态

 

posted @ 2013-06-04 17:32  Kiwi0921  阅读(284)  评论(0编辑  收藏  举报