Jump to content
<?php
include_once "removed.php";

$user = $conn -> real_escape_string($_SESSION['username']);

$query = "SELECT * FROM REMOVED WHERE removed ='$user' AND removed='1'";
$result = $conn -> query($query);

if(!$result){
  die($conn -> error);
}

else {
  $row = $result -> num_rows;

  for ($i = 0; $i < $row; ++$i)
  {
    $result -> data_seek($i);
    $row = $result -> fetch_array(MYSQLI_ASSOC);
    ?>
    <h4><?= $row['Group_name'];?></h4>
    <?php
  }
}
mysqli_close($conn);
?>

So this code that I have written has given me one issue it successfully creates the <h4> element with the $row data. However, it also creates an empty <h4> element as well.

 

Capture.PNG.24aace32d98fc4f3f50ceaccc2865484.PNG

 

I only have one entry in the database for test purposes. 

Link to post
Share on other sites

are  you sure your query only returns one result? Drop it into phpmyadmin or similar to check. 

 

edit:

 

why don't you just echo the html inside pup rather than breaking out? 

 

 echo '<h4>.$row['Group_name'].</h4>';

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to post
Share on other sites

5 minutes ago, Voids said:

I already have the query returns one result.

it might be because you're using $row for the loop and inside the loop? 

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to post
Share on other sites

Dont know why you are using a for loop for if your query is always going to return ONE result.

 

You only need to use a loop if you query may return more than ONE result, in which case you use a while loop

  while($row = $result -> fetch_assoc())
  {
    ?>
    <h4><?= $row['Group_name'];?></h4>
    <?php
  }

 

Link to post
Share on other sites

Just now, C2dan88 said:

Dont know why you are using a for loop for if your query is always going to return ONE result.

 

You only need to use a loop if you query may return more than ONE result, in which case you use a while loop

 


  while($row = $result -> fetch_assoc())
  {
    ?>
    <h4><?= $row['Group_name'];?></h4>
    <?php
  }

 

 

$row = $result -> num_rows;

  for ($i = 0; $i < $row; ++$i)
  {
    $result -> data_seek($i);
    while ($row = $result -> fetch_array(MYSQLI_ASSOC)){
      ?>
      <h4><?= $row['Group_name'];?></h4>
      <?php
    }
  }

I changed it to this and it does not create the second element. I know some lines are redundant here. But still, can someone explain why the for loop creates an empty second element, when I do not have the while statement in the code?

Link to post
Share on other sites

If you want to get just one result you add "LIMIT 1" to the end of your query.

 

For optimization purposes, you shouldn't use for, you should use while

 

 

while ($row  = $result->fetch_assoc()) {

  // do something

}

$result->free();

 

 

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×