我是php新手…请告诉我mysql_fetch_数组($n1)的作用和循环是什么
请告诉我我是php新手…请告诉我mysql_fetch_数组($n1)的作用和循环是什么,php,Php,请告诉我mysql\u fetch\u数组($n1)做什么,它循环了什么,每次循环都会给$msg分配什么值。$n1是一个mysql资源 mysql_fetch_数组将与此资源对话并返回一个数组 在这里,当您的请求选择mysql表“exp”的每一行的“name”字段时,mysql_fetch_array()将返回表中每一行的数组:[“name”=>value]。可能不是最好的解释,但无论如何: $sql="select name from exp"; $n1 = mysql_query($
mysql\u fetch\u数组($n1)
做什么,它循环了什么,每次循环都会给$msg
分配什么值。$n1是一个mysql资源
mysql_fetch_数组将与此资源对话并返回一个数组
在这里,当您的请求选择mysql表“exp”的每一行的“name”字段时,mysql_fetch_array()将返回表中每一行的数组:[“name”=>value]。可能不是最好的解释,但无论如何:
$sql="select name from exp";
$n1 = mysql_query($sql);
while ($row=mysql_fetch_array($n1)) {
$msg[]=$row['name'];
}
echo json_encode($msg);
此函数用于在每次调用时获取一行;它取决于查询本身返回的行数
这一行:
$sql="select name from exp";
$n1 = mysql_query($sql);
//$n1 returns the whole result set from your query, this means it contains
//all the values of 'name' from your table 'exp'
//mysql_fetch_array will retrieve each 'name' from the result set $n1
while ($row=mysql_fetch_array($n1)) {
//$msg is an array, writing [] makes it auto-increment its index
//depending on the order of your records, the first 'name' returned from your
//table will be assigned to $msg and the second time it loops, the second 'name'
//will be assigned and so on.
$msg[]=$row['name'];
//so this will be saved as $msg[0] = 'first name', $msg[1] = 'second name'
//and so on till the end of records
}
echo json_encode($msg);
获取$row数组,该数组填充该行的字段(name
是其中之一)。
字段由查询本身决定(“选择名称…”
)
你实际上是在说:只要有行要处理,就给我下一行。
下一个存储在$row
中,您可以处理它的字段
实际上是用字段填充数组($msg
),因此生成的数组如下所示:
while ($row=mysql_fetch_array($n1)) {
如果您是PHP新手,我建议您熟悉PHP。它的唯一目的是回答这些确切的问题。当它第二次循环时,它会被重新分配还是将元素推入$msg..@NithishReddyJ中,它不会被重新分配,因为[]
,它会自动递增到下一个数组索引。是的,它将把它推入$msg
。你能给我解释一下mysql\u fetch\u array()和mysql\u fetch\u row()的区别吗?你可以找到一些很好的解释
while ($row=mysql_fetch_array($n1)) {
$msg[0] = "some name";
$msg[1] = "another name";
...