Heim >Backend-Entwicklung >PHP-Tutorial >mysql_assoc_PHP鏁欑▼ | 甯涔嬪
[php]
function fetch_array($query, $result_type = MYSQL_ASSOC)
{
return mysql_fetch_array($query, $result_type);
}
鍙兘鍦ㄧ湅PHP椤圭洰鐨勬椂鍊欙紝鐗瑰埆鏄拰鏁版嵁搴撹繛鐢ㄧ殑鏃跺€欙紝涓€鑸殑浠g爜閮戒細鍑虹幇杩欐牱鐨勫舰寮忥紝鍙槸鐩稿綋浜庡嚱鏁版帴鍙g殑杞崲銆?/p>
鍏朵腑鏈塎YSQL_ASSOC涓€涓嬪瓙娌$湅鎳傛槸浠€涔堬紝鍚庢潵鏌ヤ簡涓嬫墜鍐岋紝绔熺劧鏄硾娉涚殑璋堜簡涓嬶細
mysql_fetch_array() 涓彲閫夌殑绗簩涓弬鏁?result_type 鏄竴涓父閲忥紝鍙互鎺ュ彈浠ヤ笅鍊硷細MYSQL_ASSOC锛孧YSQL_NUM 鍜?MYSQL_BOTH銆傛湰鐗规€ф槸 PHP 3.0.7 璧锋柊鍔犵殑銆傛湰鍙傛暟鐨勯粯璁ゅ€兼槸 MYSQL_BOTH銆?/p>
杩欒鎴戞湁鐐瑰皬绾犵粨锛屽悗鏉ョ櫨搴︿簡涓嬶紝缁堜簬鐭ラ亾鍖哄埆浜嗐€傚叾涓璏YSQL_ASSOC鏄彧鑳界敤鍏宠仈绱㈠紩锛孧YSQL_NUM鍙兘鐢ㄦ暟瀛楃储寮曪紝MYSQL_BOTH鏁板瓧銆佸叧鑱旈兘鏄彲浠ョ殑銆傦紙ps锛氳繖閲屼笉鐭ラ亾鐢ㄧ储寮曞悎涓嶅悎閫傦紝鍏跺疄涔熷氨鏄暟缁勭殑KEY浜嗭級
涓嬮潰鏄笁娈垫墜鍐屼笂鐨勪唬鐮侊細
EXAMPLE1 MYSQL_NUM
[php]
mysql_connect("localhost", "mysql_user", "mysql_password") or
die("Could not connect: " . mysql_error());
mysql_select_db("mydb");
$result = mysql_query("SELECT id, name FROM mytable");
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
printf ("ID: %s Name: %s", $row[0], $row[1]);
}
mysql_free_result($result);
?>
EXAMPLE2 MYSQL_ASSOC
[php]
mysql_connect("localhost", "mysql_user", "mysql_password") or
die("Could not connect: " . mysql_error());
mysql_select_db("mydb");
$result = mysql_query("SELECT id, name FROM mytable");
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
printf ("ID: %s Name: %s", $row["id"], $row["name"]);
}
mysql_free_result($result);
?>
EXAMPLE MYSQL_BOTH
[php]
mysql_connect("localhost", "mysql_user", "mysql_password") or
die("Could not connect: " . mysql_error());
mysql_select_db("mydb");
$result = mysql_query("SELECT id, name FROM mytable");
while ($row = mysql_fetch_array($result, MYSQL_BOTH)) {
printf ("ID: %s Name: %s", $row[0], $row["name"]);
}
mysql_free_result($result);
?>
鍏朵腑MYSQL_BOTH
[php]
printf ("ID: %s Name: %s", $row["id"], $row["name"]);
lt;pre name="code" class="php"> printf ("ID: %s Name: %s", $row["id"], $row[1]);