php basics
PHP mysql_fetch_array function
The PHP mysql_fetch_array function is an extended function of mysql_fetch_row . It adds a parameter. The result set of mysql_query can be stored as a result of PHP Array in the form of a numeric index or a related index (with a field as a key name) . It provides more than mysql_fetch_row Richer application methods and convenient calling.
Basic syntax of PHP mysql_fetch_array function
array mysql_fetch_array ( resource $result , int $result_type )
There are two parameters in the basic syntax. The first parameter $result is the result set of mysql_query , which is a necessary item, and the second parameter $result_type is the key to control the storage key name. There are three options available, as shown below.- MYSQL_NUM-get only numeric index (similar to the result of mysql_fetch_row )
- MYSQL_BOTH-The default value of mysql_fetch_array, which can be a numeric index or a related index
- MYSQL_ASSOC-get only related indexes (indexes are fields)
PHP mysql_fetch_array function example 1. Use MYSQL_NUM
<?
mysql_connect("localhost", "mysql_user", "mysql_password") or die("Could not connect DB.");
mysql_select_db("TestDB");
$result = mysql_query("SELECT test_id,test_name FROM topic");
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
printf ("TID: %s NAME: %s", $row[0], $row[1]);
}
?>
The first example is stored in a numeric index, so when you finally call the result, you can directly use the wording of $row[0] and $row[1], but you cannot use $row[test_id] and $row[test_name]. Writing.mysql_connect("localhost", "mysql_user", "mysql_password") or die("Could not connect DB.");
mysql_select_db("TestDB");
$result = mysql_query("SELECT test_id,test_name FROM topic");
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
printf ("TID: %s NAME: %s", $row[0], $row[1]);
}
?>
PHP mysql_fetch_array function example 2: Use MYSQL_BOTH
<?
mysql_connect("localhost", "mysql_user", "mysql_password") or die("Could not connect DB.");
mysql_select_db("TestDB");
$result = mysql_query("SELECT test_id,test_name FROM topic");
while ($row = mysql_fetch_array($result, MYSQL_BOTH)) {
printf ("TID: %s NAME: %s", $row[0], $row[1]);
}
?>
The second example uses the MYSQL_BOTH parameter, so when calling, it is feasible to use $row[0], $row[1] or $row[test_id], $row[test_name].mysql_connect("localhost", "mysql_user", "mysql_password") or die("Could not connect DB.");
mysql_select_db("TestDB");
$result = mysql_query("SELECT test_id,test_name FROM topic");
while ($row = mysql_fetch_array($result, MYSQL_BOTH)) {
printf ("TID: %s NAME: %s", $row[0], $row[1]);
}
?>
PHP mysql_fetch_array function example 3. Use MYSQL_ASSOC
<?
mysql_connect("localhost", "mysql_user", "mysql_password") or die("Could not connect DB.");
mysql_select_db("TestDB");
$result = mysql_query("SELECT test_id,test_name FROM topic");
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
printf ("TID: %s NAME: %s", $row[test_id], $row[test_name]);
}
?>
Example 3 uses the MYSQL_ASSOC parameter, so in the final call, you can only use the wording of $row[test_id], $row[test_name], and the call method using numeric index will not be able to get the array value.mysql_connect("localhost", "mysql_user", "mysql_password") or die("Could not connect DB.");
mysql_select_db("TestDB");
$result = mysql_query("SELECT test_id,test_name FROM topic");
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
printf ("TID: %s NAME: %s", $row[test_id], $row[test_name]);
}
?>
The above is the usage of PHP mysql_fetch_array function and the difference of the three parameters. Please adjust it according to your needs.
Post a Comment
0 Comments