----- 022-mysql.php -----
<!DOCTYPE html> <html> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8"> <title>MySQL</title> </head> <body> <h2>MySQL</h2> <pre style="font-family:微软雅黑; font-size:14pt"> <?php //绝对路径!绝对路径!绝对路径! $con = @mysql_connect("localhost", "root", "root") or die("无法连接数据库"); mysql_select_db("world", $con); $sql = "SELECT code, name, population FROM country ORDER BY population DESC LIMIT 5"; $result = mysql_query($sql, $con); echo "查询到的资源:", $result, "\n"; echo "查询到的结果数:", @mysql_numrows($result), "\n"; echo "第三行的数据:", mysql_result($result, 0, "code"), " ", mysql_result($result, 0, "name"), " ", mysql_result($result, 0, "population"), "\n"; mysql_data_seek($result, 0); //指针归位 echo "<b>mysql_fetch_row查询数据:</b>", "\n"; while(list($a, $b, $c) = mysql_fetch_row($result)){ echo $a, "---", $b, "---", $c, "\n"; } mysql_data_seek($result, 0); //指针归位 echo "<b>mysql_fetch_assoc查询数据:</b>", "\n"; while($s = mysql_fetch_assoc($result)){ var_export($s); } echo "\n"; mysql_data_seek($result, 0); //指针归位 echo "<b>mysql_fetch_array查询数据:</b>", "\n"; while($s = mysql_fetch_array($result, MYSQL_NUM)){ echo $s[0], " ", $s[1], " ", $s[2]/100000000, "亿\n"; } mysql_data_seek($result, 0); //指针归位 echo "<b>mysql_fetch_object查询数据:</b>", "\n"; while($s = mysql_fetch_object($result)){ foreach($s as $key=>$value){ printf("%s=>%-15s ", $key, $value); //echo $key, "=>", $value, "\t"; } echo "\n"; } echo "错误信息:", var_export(mysql_error()), "\n"; echo "错误代码:", mysql_errno(), "\n"; ?> </pre> </body> </html>