为啥PHP的while循环是重复出现同一个数据呢?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了为啥PHP的while循环是重复出现同一个数据呢?相关的知识,希望对你有一定的参考价值。

<?php session_start();$kw = $_POST['kw'];if($kw=="") header("location:message.php");$_SESSION['em']="keywords is empty!";return false;$con = mysql_connect('localhost','root','1234');if(!$con) echo "CONNECT MYSQL FAIL!";return false;mysql_select_db("search");$sql = "select * from keywords where title like '%".$kw."%'";//SQL语句$sqlresult = mysql_query($sql);//发送SQL命令$list = mysql_fetch_assoc($sqlresult);//作为关联数组$listnum = mysql_num_rows($sqlresult);//获取行数//循环开始$i=0;while($i<$listnum) $i++;echo $list['title']."</br>";mysql_close($con);?>我的想法是通过获取数据集返回的行数,来决定循环次数。但是却出现了重复出现同一条数据。这是为什么呢???正确的循环写法应该怎么写?为什么要这样写?搜索baidu应该是有3条匹配的数据:一个是baidu一个是baidu BCC、还有BaiduSearch数据库在图片中

根据 php 手册中,对于 fetch_assoc 函数的说明:


mysqli_fetch_assoc -- Fetch a result row as an associative array


这个函数的功能,是从数据库查询的结果集中,取一条(行)记录作为关联数组返回。


如果想要显示结果集中所有的数据,一是可以使用 fetch_all 函数。


如果是 mysqli_fetch_assoc ,则通常的代码是:

if ($result = mysqli_query($link, $query)) 

    /* fetch associative array */
    while ($row = mysqli_fetch_assoc($result)) 
        printf ("%s (%s)\\n", $row["Name"], $row["CountryCode"]);
    

    /* free result set */
    mysqli_free_result($result);
参考技术A $list = mysql_fetch_assoc($sqlresult);
要放到循环中去
查询结果有多行时,
mysql_fetch_assoc();,该函数返回当前行数据,同时把标记移动到下一行。
所以要放在循环里。

While 循环与 || 一起使用但不是&&,不明白为啥?

【中文标题】While 循环与 || 一起使用但不是&&,不明白为啥?【英文标题】:While Loop Works with || but not &&, Not Understanding Why?While 循环与 || 一起使用但不是&&,不明白为什么? 【发布时间】:2021-05-20 10:45:38 【问题描述】:

程序的目标是掷两个骰子,直到它们都等于 6。当我在 while 语句中使用 && 时,它会在一个骰子等于 6 后停止。这是为什么呢?不应该 && 测试第一个条件然后如果它是正确的测试第二个?

while ((diceOne != 6) || (diceTwo != 6)) 
    diceOne = numberGeneration.Next(1, 7);
    diceTwo = numberGeneration.Next(1, 7);
    attempt = ++attempt;

    Console.WriteLine("Your first dice is: " + diceOne + " your second dice is: " + diceTwo);
    Console.WriteLine("Press any button to continue");
    Console.ReadKey();



Console.WriteLine("It took you " + attempt);
Console.WriteLine("Press any key to continue");
Console.ReadKey();

【问题讨论】:

【参考方案1】:

它停止是因为你打破了一个骰子不同于 6 的条件。

while ((diceOne != 6) && (diceTwo != 6))

While 将在两个条件都为真时执行。

【讨论】:

【参考方案2】:

我同意@Pedro,您也可以在while(true) 循环中使用break 语句并在两个骰子都等于6 时停止循环:

while (true) 
    diceOne = numberGeneration.Next(1, 7);
    diceTwo = numberGeneration.Next(1, 7);
    attempt = ++attempt;

    Console.WriteLine("Your first dice is: " + diceOne + " your second dice is: " + diceTwo);
    Console.WriteLine("Press any button to continue");
    Console.ReadKey();
    if (diceOne == 6 && diceTwo == 6) break;



Console.WriteLine("It took you " + attempt);
Console.WriteLine("Press any key to continue");
Console.ReadKey();

【讨论】:

【参考方案3】:

注意while循环条件是继续循环的必要和充分条件。 (diceOne != 6) &amp;&amp; (diceTwo != 6)) 说“两个骰子都不是 6”。当两个骰子都不是6时,你当然要继续循环,所以这是充分条件,但这不是必要条件。您还想在什么时候继续循环?当一个骰子是 6 而一个骰子不是!

继续循环的充分必要条件是至少有一个骰子不是6,这就是||所表达的。

【讨论】:

【参考方案4】:

其他用户已经向您解释了为什么它不起作用。我可能会建议像这样修改 while 语句:

while (!(diceOne == 6 && diceTwo == 6))

【讨论】:

问题为什么,所以这不能回答问题。 你是对的,就像我在回答中所说的那样,其他用户已经回答了这个问题。无论如何,我的示例可能对 OP 有用,因为(这是我的拙见)是以正确的方式编写条件的更直接的方法。

以上是关于为啥PHP的while循环是重复出现同一个数据呢?的主要内容,如果未能解决你的问题,请参考以下文章

没有退出“while”循环,但为啥呢?

为啥这个while循环在输入的每个字符之后重复?

PHP / MySQL:如何使用WHILE循环插入数据库[重复]

php 数据循环重复

为啥while循环的执行速度会随时间变化?

为啥javascript不会在while循环中堆叠