For a long time, many beginners have been confused about the difference between these functions in Mysql that obtain data from the query result set, especially the difference between mysql_fetch_array and mysql_fetch_assoc. Well, it seems to be the same when used together, but if you use it incorrectly, an error will be reported, which is very difficult for novice developers! So today we will talk about it with examples!
There is no big difference between fetch_array() and fetch_assoc(). The main thing is how to use it?
fetch_array() can fetch all the results in the result set if it is used alone as the condition of while. If it is applied to the result set alone, only one row of the database result set can be taken out from the result set.
For example:
<?php //设置页面显示的文字编码 header("Content-Type:text/html;charset=utf-8"); //设置默认显示新闻的条数 $number = 20; //从GET参数判断是否需要对显示新闻条数进行修改 if (count($_GET)>0) { $number = $_GET('number'); } //连接数据库 $con = mysql_connect("localhost","root","root"); //设置数据库的编码方式,一定要与数据库的编码方式相同 mysql_query("set names utf8"); //json格式的字符串 if ($con) { //选择要使用的数据库 mysql_select_db("news",$con); //数据库查询语句 $query = "SELECT * FROM news_List,news_Neirong WHERE news_List.id = news_Neirong.id ORDER BY news_List.id"; $result = mysql_query($query);//执行查询操作,会返回一个包含所有筛选结果的结果集。 //$row = mysql_fetch_array($result); while ($row = mysql_fetch_array($result)) {//mysql_fetch_array从结果集中取得一行作为关联数组或者数字数组。 echo $row['title']; //这里不能直接写"echo $row",这样写会出现错误。 } } else { echo "服务器失败了"; } mysql_close(); ?>
The return result is as follows:Sun Wukong 1 Sun Wukong 2 Sun Wukong 3 Sun Wukong 4 Sun Wukong 5 Sun Wukong
If it is inappropriate The conditional statement for while
<?php //设置页面显示的文字编码 header("Content-Type:text/html;charset=utf-8"); //设置默认显示新闻的条数 $number = 20; //从GET参数判断是否需要对显示新闻条数进行修改 if (count($_GET)>0) { $number = $_GET('number'); } //连接数据库 $con = mysql_connect("localhost","root","root"); //设置数据库的编码方式,一定要与数据库的编码方式相同 mysql_query("set names utf8"); //json格式的字符串 if ($con) { //选择要使用的数据库 mysql_select_db("news",$con); //数据库查询语句 $query = "SELECT * FROM news_List,news_Neirong WHERE news_List.id = news_Neirong.id ORDER BY news_List.id"; $result = mysql_query($query);//执行查询操作 $row = mysql_fetch_array($result); echo $row['title']; } else { echo "服务器失败了"; } mysql_close(); ?>
Only outputs one result: Monkey King 1
The above describes the content of this article, but in fact it does not explain mysql_fetch_array in detail What are the detailed differences with mysql_fetch_assoc? I hope you can study it yourself with examples, because this way we will remember it more firmly!
Recommended similar articles:
Recommended 10 articles about the mysql_fetch_array() function
The above is the detailed content of Describe the differences between mysql_fetch_array and mysql_fetch_assoc. For more information, please follow other related articles on the PHP Chinese website!