为什么 select * 来自 return 只有第一个字段?
why select * from return only the first field?
我正在对数据库进行下一次查询
$result = $con->query ( 'select * from table');
$datos = $result->fetch_assoc();
echo "Cantidad de datos: ".count($datos).",";
print_r($datos);
应该显示一个包含所有条目的数组,但只显示第一个条目。为什么?
PS: 我看了其他帖子,但我没有限制或加入。
fetch_assoc fetches a result row as an associative array
所以您可以使用一个 while 循环遍历所有行,如果可能的话获取另一行。
$count = 0;
while( $row = $result->fetch_assoc() ){
// You can access data like this -->> $row['data'];
$count++;
}
echo $count;
完成后,您应该释放与结果相关的内存
$result->free();
但是如果您只想获得计数,您可以使用 mysql_num_rows 结果集中的 returns 行数。
$count = mysql_num_rows($result);
echo $count;
fetch_assoc
returns你做的时候只有一行$datos = $result->fetch_assoc();
你可以在PDO
和mysqli
中获取整个数组,这里是一个使用 mysqli->fetch_all 函数获取所有行的示例,希望对您有所帮助!
//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);
请始终参阅所用框架的手册。 fetch_assoc();获取结果行作为关联数组。如果要获取所有行,请使用如下 while 语句:
$result = $c->query('SELECT user,host FROM mysql.user');
while ($row = $result->fetch_assoc()) {
printf("'%s'@'%s'\n", $row['user'], $row['host']);
我正在对数据库进行下一次查询
$result = $con->query ( 'select * from table');
$datos = $result->fetch_assoc();
echo "Cantidad de datos: ".count($datos).",";
print_r($datos);
应该显示一个包含所有条目的数组,但只显示第一个条目。为什么?
PS: 我看了其他帖子,但我没有限制或加入。
fetch_assoc fetches a result row as an associative array
所以您可以使用一个 while 循环遍历所有行,如果可能的话获取另一行。
$count = 0;
while( $row = $result->fetch_assoc() ){
// You can access data like this -->> $row['data'];
$count++;
}
echo $count;
完成后,您应该释放与结果相关的内存
$result->free();
但是如果您只想获得计数,您可以使用 mysql_num_rows 结果集中的 returns 行数。
$count = mysql_num_rows($result);
echo $count;
fetch_assoc
returns你做的时候只有一行$datos = $result->fetch_assoc();
你可以在PDO
和mysqli
中获取整个数组,这里是一个使用 mysqli->fetch_all 函数获取所有行的示例,希望对您有所帮助!
//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);
请始终参阅所用框架的手册。 fetch_assoc();获取结果行作为关联数组。如果要获取所有行,请使用如下 while 语句:
$result = $c->query('SELECT user,host FROM mysql.user');
while ($row = $result->fetch_assoc()) {
printf("'%s'@'%s'\n", $row['user'], $row['host']);