简体   繁体   中英

How to stop loop in while statement in PHP MySQL?

I would like to ask.

Is it okay to add exit(); at the end of each statement or is there any good way besides exit(); ?

The reason is stop the display data from looping, Because if I remove/disable exit(); , the data will loop and display all the row available in database table. And else statement will also execute if the exit(); remove from the code.

The code as below :

<?php
    $connect      = mysqli_connect("localhost", "root", "", "database");
    global $connect;   

    if(isset($_POST['Submit'])){
        $input       = $_POST['input'];

        $sql = "SELECT * FROM table";
        $get = mysqli_query($connect,$sql);
        if($get && mysqli_num_rows($get) > 0 ){ 
            while($row = mysqli_fetch_assoc($get)){
                $input_db  = $row['input'];
                $output_db = $row['output'];

                if (($input >= $input_db) && ($output <= $output_db))
                {
                    echo "Input and output is in between";
                    exit();
                }
                else
                {
                    echo "Data added ! <br/>";
                    exit();
                }
            }
        mysqli_free_result($get);   
        }
        else
        {
            echo "data is outside range";
        }
    }
?>
<form action="testdate.php" method="post">  
    <table> 
        <tr>
            <td><i class="fa fa-unlock-alt"></i> </td>
            <td>Input : </td>
            <td><input type ="text" name="input" size="30"></td>
        </tr>
    </table>    

    <p><input class="btnSuccess" type ="submit" name="Submit" value="Submit"> </p>              
</form>

Thanks.

To directly answer the question, you can simply swap exit(); with break; . That halts a loop without exiting:

// Let's go infinite..

while(true)
{

 echo 'This only appears once, because we\'re breaking out of the loop.';

 // Not today!
 break;
}

// Everything is normal again down here.
echo 'That was a close one!';

In your specific case, the much better approach is to only select the data you need , otherwise MySQL will be busy sending the entire table only for PHP to drop most of the data it's receiving. Something like this:

<?php
$connect      = mysqli_connect("localhost", "root", "", "database");
global $connect;   

if(isset($_POST['Submit']))
{
    $input       = (int)$_POST['input']; // * See note below

    $sql = "SELECT * FROM table where ".$input.">=`input` and ".$output."<=`output`";

    $get=mysqli_query($connect,$sql);

    if($get && $get->num_rows)
    {
        echo "Input and output is in between";
    }
    else
    {
        echo "data is outside range";
    }
}
?>
  • That (int) is a simplistic security measure - it prevents someone from performing SQL injections. You'd need to do it to $output too. Use prepared queries instead if possible.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM