Php 为什么选择*只返回第一个字段?

Php 为什么选择*只返回第一个字段?,php,sql,fetch,Php,Sql,Fetch,我正在对数据库进行下一个查询 $result = $con->query ( 'select * from table'); $datos = $result->fetch_assoc(); echo "Cantidad de datos: ".count($datos).","; print_r($datos); 应显示包含所有项的数组,但仅显示第一个项。为什么? 附言:我看过其他帖子,但我没有限制或加入 fetch_assoc以关联数组的形式获取结果行 因此,您可以使用whil

我正在对数据库进行下一个查询

$result = $con->query ( 'select * from table');
$datos = $result->fetch_assoc();
echo "Cantidad de datos: ".count($datos).",";
print_r($datos);
应显示包含所有项的数组,但仅显示第一个项。为什么?

附言:我看过其他帖子,但我没有限制或加入

fetch_assoc以关联数组的形式获取结果行

因此,您可以使用while循环遍历所有行,如果可能的话,获取另一行

$count = 0;
while( $row = $result->fetch_assoc() ){
    // You can access data like this -->> $row['data'];
    $count++;
}
echo $count;
完成后,应该释放与结果相关的内存

$result->free();
但若您只想获得count,那个么可以使用mysql\u num\u rows从结果集中返回行数

$count = mysql_num_rows($result);
echo $count;

fetch_assoc
在执行
$datos=$result->fetch_assoc()时只返回一行
您可以在
PDO
mysqli
中获取整个数组,下面是一个使用函数获取所有行的示例,希望这对您有所帮助

//Database Connection
$sqlConn =  new mysqli($hostname, $username, $password, $database);

//Build SQL String
$sqlString = "SELECT * FROM my_table";

//Execute the query and put data into a result
$result = $this->sqlConn->query($sqlString);

//Copy result into a associative array
$resultArray = $result->fetch_all(MYSQLI_ASSOC);

//Copy result into a numeric array
$resultArray = $result->fetch_all(MYSQLI_NUM);

//Copy result into both a associative and numeric array
$resultArray = $result->fetch_all(MYSQLI_BOTH);

请始终参考您正在使用的框架手册。获取联合体();以关联数组的形式获取结果行。如果要获取所有行,请使用如下while语句:

    $result = $c->query('SELECT user,host FROM mysql.user');
    while ($row = $result->fetch_assoc()) {
    printf("'%s'@'%s'\n", $row['user'], $row['host']);

fetch_assoc()
仅返回单个数组。执行
$datos=$result->fetch_assoc()操作时,仅获取第一条记录
。可能
获取数组
?回答得不错,但可能解释了原因。添加了一点解释,希望没问题。