Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/232.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php mysql查询没有从表中获取所有元素_Php_Mysql - Fatal编程技术网

Php mysql查询没有从表中获取所有元素

Php mysql查询没有从表中获取所有元素,php,mysql,Php,Mysql,它只是从表中获取第一个元素。 表名为categories,包含两列:id、category 我不明白为什么它只从表中取第一行 <?php $sql = "SELECT category FROM categories"; $result = mysql_query($sql); $row = mysql_fetch_assoc($result); //print_r($row); ?> 就像你写的mysql\u fet

它只是从表中获取第一个元素。 表名为categories,包含两列:id、category

我不明白为什么它只从表中取第一行

<?php 
    $sql = "SELECT category FROM categories";
    $result = mysql_query($sql);
    $row = mysql_fetch_assoc($result); 
    //print_r($row);             
?>


就像你写的
mysql\u fetch\u assoc($result)将仅获取一行。您必须使用循环来获取所有内容。

就像您编写的
mysql\u fetch\u assoc($result)将仅获取一行。您必须使用循环来获取所有行。

您需要对结果集进行迭代,以便检索所有行

while($row = mysql_fetch_assoc($result)) {
    print($row);
}

另外,请停止使用
mysql\uu
函数。它们不再得到维护。看到了吗?相反,学习,并使用or-将帮助您决定哪一个

您需要对结果集进行迭代,以便检索所有行

while($row = mysql_fetch_assoc($result)) {
    print($row);
}

另外,请停止使用
mysql\uu
函数。它们不再得到维护。看到了吗?相反,学习,并使用or-将帮助您决定哪一个

如果只调用一次mysql\u fetch\u assoc,您将只得到第一行

while(false !== $row = mysql_fetch_assoc($result)) {
    print_r($row)
}

如果只调用mysql\u fetch\u assoc一次,您将只得到第一行

while(false !== $row = mysql_fetch_assoc($result)) {
    print_r($row)
}

在while循环中使用此选项:

while ($row = mysql_fetch_assoc($result)) {
    print_r($row);
}

在while循环中使用此选项:

while ($row = mysql_fetch_assoc($result)) {
    print_r($row);
}

您正在使用的函数只执行一条记录。 尝试while($row=mysqli\u fetch\u数组($result)) { echo$row['id']。“”.$row['category']; 回声“
”;
}

您正在使用的函数只执行一条记录。 尝试while($row=mysqli\u fetch\u数组($result)) { echo$row['id']。“”.$row['category']; 回声“
”;
}

要检索结果集,请根据需要使用循环

 foreach
在遍历长度未知(或可能未知)的数组时使用。 作为

为了

在遍历已设置长度的数组时使用,或者在需要计数器时使用

for(i=0;i<sizeof($row);i++)
{
echo $row[$i];
}

要检索结果集,请根据需要使用循环

 foreach
在遍历长度未知(或可能未知)的数组时使用。 作为

为了

在遍历已设置长度的数组时使用,或者在需要计数器时使用

for(i=0;i<sizeof($row);i++)
{
echo $row[$i];
}

while($row=mysql\u fetch\u assoc($result)){//do stuff}
while($row=mysql\u fetch\u assoc($result)){//do stuff}
谢谢,我期待着使用MySQLi。谢谢,我期待着使用MySQLi。