PHP - Page updates non-stop

Asked

Viewed 30 times

-1

Guys, I want to understand why this is happening,

I used this code;

       <table class="table table-striped  table-hover" id="dataTables-example">
                                <thead>
                                    <tr>
                                        <th>Name</th>
                                        <th>Email </th>
                                        <th>Senha</th>
                                      <th></th>
                                    </tr>
                                </thead>
                                <tbody>

                               
                  <?php 


                   include 'database.php';
                   $pdo = Database::connect();
                   $sql = 'SELECT * FROM users ORDER BY id DESC';
                  

                   foreach ($pdo->query($sql) as $row) {
                            echo '<tr>';
                            echo '<td>'. $row['zname'] . '</td>';
                            echo '<td>'. $row['email'] . '</td>';
                            echo '<td>'. $row['password'] . '</td>';
                          
                            echo '<td width=100%>';
                           
                            echo '</td>';
                            echo '</tr>';

                   }if ($row >= 1){

    $sxURL="tem pedido.php";
  echo ("<script>location.href='$sxURL'</script>");

   }
  else if ($row == 0){

 $fxURL="não tem pedido.php";
  echo ("<script>location.href='$fxURL'</script>");
  
 }else {

   $zxURL="Em análise.php";
  echo ("<script>location.href='$zxURL'</script>");

}

Let’s say you go to the status page ( >=1 ) "you have asked.php" ok, but you keep refreshing non-stop.

how to solve this? what is wrong with this code.

2 answers

-1

According to your code, the "$Row" variable does not store the total query records. It is an array created during the iteration of the results made by foreach.

You would need to change your code in order to count the results. This could be done as follows:

$pdo = Database::connect();
$sql = 'SELECT * FROM users ORDER BY id DESC';
$pdo->prepare($sql);    
$pdo->execute();
$resultado = $pdo->fetchAll();
$total = count($resultado);
                   

The "$result" variable will have an array with the results of your query. The "Count" function allows counting the number of array elements, which are the total of rows returned by the query.

Thus, either the content of "$total" is zero (the query did not return records) or greater than zero (the query returned records). In this way, your code could look like this:

<?php 
include 'database.php';
$pdo = Database::connect();
$sql = 'SELECT * FROM users ORDER BY id DESC';
$pdo->prepare($sql);    
$pdo->execute();
$resultado = $pdo->fetchAll();
$total = count($resultado);
foreach ($resultado as $row) {
    echo '<tr>';
    echo '<td>'. $row['zname'] . '</td>';
    echo '<td>'. $row['email'] . '</td>';
    echo '<td>'. $row['password'] . '</td>';
    echo '<td width=100%>';
    echo '</td>';
    echo '</tr>';
}
if ($total >= 1){
    $sxURL="possui_registros.php";
    echo ("<script>location.href='$sxURL'</script>");
} else {
    $fxURL="nao_possui_registros.php";
    echo ("<script>location.href='$fxURL'</script>");
}
?>

               

-3

1º $sxURL = is not a valid path because it has SPACE.

2nd if you want to make a redirect the best option is this:

header('Location: '.$sxURL);
exit();

OBS: remember the protocol if necessary.

  • Hello, Syntax seems to be correct but this error appears: Fatal error: Uncaught Error: Call to undefined method PDO::execute() in

  • Beauty adjusted on top of error and it worked $q = $pdo->prepare($sql); $q->execute(array($id));&#xA;$resultado = $q->fetchAll(); the syntax is totally correct and worked, Thank you!!

  • The function header() won’t work!!! To set headers on the page you need no html in the output. It has already displayed a <table>, then the function will return Cannot modify header information - headers already sent by

Browser other questions tagged

You are not signed in. Login or sign up in order to post.