php -- 连接Mysql 数据库

时间:2014-05-25 03:24:55   收藏:0   阅读:281

----- 022-mysql.php -----

bubuko.com,布布扣
 1 <!DOCTYPE html>
 2 <html>
 3 <head>
 4     <meta http-equiv="content-type" content="text/html; charset=utf-8">
 5     <title>MySQL</title>
 6 </head>
 7 <body>
 8 <h2>MySQL</h2>
 9 <pre style="font-family:微软雅黑; font-size:14pt">
10 <?php
11     //绝对路径!绝对路径!绝对路径!
12     $con = @mysql_connect("localhost", "root", "root") or die("无法连接数据库");
13     mysql_select_db("world", $con);
14     $sql = "SELECT code, name, population FROM country ORDER BY population DESC LIMIT 5";
15     $result = mysql_query($sql, $con);
16     echo "查询到的资源:", $result, "\n";
17     echo "查询到的结果数:", @mysql_numrows($result), "\n";
18     echo "第三行的数据:", mysql_result($result, 0, "code"), " ", mysql_result($result, 0, "name"), " ", mysql_result($result, 0, "population"), "\n";
19 
20     mysql_data_seek($result, 0); //指针归位
21     echo "<b>mysql_fetch_row查询数据:</b>", "\n";
22     while(list($a, $b, $c) = mysql_fetch_row($result)){
23         echo $a, "---", $b, "---", $c, "\n";
24     }
25 
26     mysql_data_seek($result, 0); //指针归位
27     echo "<b>mysql_fetch_assoc查询数据:</b>", "\n";
28     while($s = mysql_fetch_assoc($result)){
29         var_export($s);
30     }
31     echo "\n";
32 
33     mysql_data_seek($result, 0); //指针归位
34     echo "<b>mysql_fetch_array查询数据:</b>", "\n";
35     while($s = mysql_fetch_array($result, MYSQL_NUM)){
36         echo $s[0], " ", $s[1], " ", $s[2]/100000000, "亿\n";
37     }
38 
39     mysql_data_seek($result, 0); //指针归位
40     echo "<b>mysql_fetch_object查询数据:</b>", "\n";
41     while($s = mysql_fetch_object($result)){
42         foreach($s as $key=>$value){
43             printf("%s=>%-15s ", $key, $value);
44             //echo $key, "=>", $value, "\t";
45         }
46         echo "\n";
47     }
48     echo "错误信息:", var_export(mysql_error()), "\n";
49     echo "错误代码:", mysql_errno(), "\n";
50 ?>
51 </pre>
52 </body>
53 </html>
bubuko.com,布布扣

bubuko.com,布布扣

php -- 连接Mysql 数据库,布布扣,bubuko.com

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!