我是新手,有个问题请帮忙,就是用mysqli类,数据表test中有5行数据,用foreach函数只能取出第一行,其他的都取不出来,而用while就可以取出5行,为何?
$mysqlii=new mysqli("localhost","root","root","test");
$sql="select * from user";
$rs=$mysqlii->query($sql,MYSQLI_STORE_RESULT);
foreach($rs->fetch_row() as $key=>$v2){
print_r( $v2); //这里只能取出第一行数据
}
//while(list($a,$b,$c,$d)=$rs->fetch_array()) 能够取出5行即全部数据
//
//echo $a."+".$b."+".$c."
";
?>
回复讨论(解决方案)
foreach($rs->fetch_row() as $key=>$v2){
print_r( $v2); //这里只能取出第一行数据
}
这样写实现的功能是:循环输出第一行的所有列的数据。
$rs->fetch_row()返回的是数组。
foreach($rs->fetch_row() as $key=>$v2){
print_r( $v2); //这里只能取出第一行数据
}
foreach($rs->fetch_array() as $key=>$v2){
print_r( $v2); //这里只能取出第一行数据
}
你的$rs->fetch_row()就只有一行数据,要用while循环循环读取出来。
foreach($rs->fetch_row() as $key=>$v2){
print_r( $v2); //这里只能取出第一行数据
}
这样写实现的功能是:循环输出第一行的所有列的数据。
$rs->fetch_row()返回的是数组。
我也考虑过是个二维数组,用
foreach($rs->fetch_row() as $v2){
foreach($v2 as $v3){
print_r( $v3);
}
}
但是报错
foreach($rs->fetch_row() as $key=>$v2){ print_r( $v2); //这里只能取出第一行数据 }
你foreach()里面的东西本身就有问题了,$rs->fetch_row()只取出了一行吧
错误提示:Warning: Invalid argument supplied for foreach() in D:\www\mysqli.php on line 13
网上说foreach的效率while高,所以用foreach,如果问题出在re->fetch_array这里,那while应该也不会读出来多行数据?
引用 1 楼 yupengbo 的回复:
foreach($rs->fetch_row() as $key=>$v2){
print_r( $v2); //这里只能取出第一行数据
}
这样写实现的功能是:循环输出第一行的所有列的数据。
$rs->fetch_row()返回的是数组。
我也考虑过是个二维数组,用
foreach($rs->fetch_row() as ……
这个还是不对啊!
foreach($rs->fetch_row() as $v2)
当你打印$v2的时候,打印出来的是第一行中的每一列的数据,
是个字符串,不是数组。
这种情况下,你再foreach($v2 as $v3)的话肯定会报错啊!
while($ary=$rs->fetch_row())
{
foreach($ary as $key=>$v2){
print_r($v2);
}
}
这样是可以的。
yupengbo 说的很对,是不是可以得出个结论while是可以行数据和列数据都遍历,而foreach只能遍历列数据????
可以这样理解 $rs->fetch_row() 这个相当于一个指针 第一次指向 0
所以while读取$rs->fetch_row() 是有数据出来的,读出来继续下一次的时候 $rs->fetch_row()这个指针会向下移一位。直到读不出数据才会结束。
foreach($rs->fetch_row() as $v2)里面的$rs->fetch_row() 仅仅当作一个数组来处理 把里面的一行数组读完就结局了。不会在循环的从$rs->fetch_row() 读取数据
这样应该可以一次性读取出来
foreach($rs->fetch_all() as $key=>$v2){
print_r( $v2); //这里只能取出第一行数据
}
嗯,
foreach是在数组内部循环,
注意你第一个foreach那个循环, fetch_row只运行了一次,
而while那个循环,fetch_array是运行了5次
无语,你应该写
foreach($rs as $row)
printf_r($row);

PHP is used to build dynamic websites, and its core functions include: 1. Generate dynamic content and generate web pages in real time by connecting with the database; 2. Process user interaction and form submissions, verify inputs and respond to operations; 3. Manage sessions and user authentication to provide a personalized experience; 4. Optimize performance and follow best practices to improve website efficiency and security.

PHP uses MySQLi and PDO extensions to interact in database operations and server-side logic processing, and processes server-side logic through functions such as session management. 1) Use MySQLi or PDO to connect to the database and execute SQL queries. 2) Handle HTTP requests and user status through session management and other functions. 3) Use transactions to ensure the atomicity of database operations. 4) Prevent SQL injection, use exception handling and closing connections for debugging. 5) Optimize performance through indexing and cache, write highly readable code and perform error handling.

Using preprocessing statements and PDO in PHP can effectively prevent SQL injection attacks. 1) Use PDO to connect to the database and set the error mode. 2) Create preprocessing statements through the prepare method and pass data using placeholders and execute methods. 3) Process query results and ensure the security and performance of the code.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP makes it easy to create interactive web content. 1) Dynamically generate content by embedding HTML and display it in real time based on user input or database data. 2) Process form submission and generate dynamic output to ensure that htmlspecialchars is used to prevent XSS. 3) Use MySQL to create a user registration system, and use password_hash and preprocessing statements to enhance security. Mastering these techniques will improve the efficiency of web development.

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Chinese version
Chinese version, very easy to use

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version
Useful JavaScript development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment