0

I have some PHP code that sets variables from a database. I'll explain, please let me know if it does not make sense.

So, I have a query to select * from the table if class = '$class'. That all works I got it working.

I then set the variables like this $id = $row["id"]; and that all works, in my HTML code I have <p><?php echo $id?></p> and if their class = $class it will display it, however, if it does not meet those requirements the variables are not set, so I get the error Notice: Undefined variable: id in C:\wamp64\www\studentplanner\account\homework.php on line 73.

What I want to do is only output the results in HTML if the requirement was met.

No idea if that makes sense or not!

$sql = "SELECT * FROM homework WHERE class = '$class'";
$result = mysqli_query($conn, $sql);

if (mysqli_num_rows($result) > 0) {
    // output data of each row
    while($row = mysqli_fetch_assoc($result)) {
      $id = $row["id"];
      $teacher_set = $row["teacher_set"];
      $class = $row["class"];
      $name = $row["name"];
      $description = $row["description"];

    }
}


          <p><?php echo $id?></p>
          <p><?php echo $teacher_set?></p>
          <p><?php echo $class?></p>
          <p><?php echo $name?></p>
          <p><?php echo $description?></p>
B. Desai
  • 16,414
  • 5
  • 26
  • 47
Tom
  • 1
  • 4
  • You need to show us the actual code. – M. Eriksson Nov 18 '17 at 09:40
  • Possible duplicate of [PHP: "Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset"](https://stackoverflow.com/questions/4261133/php-notice-undefined-variable-notice-undefined-index-and-notice-undef) – M. Eriksson Nov 18 '17 at 09:40
  • no @MagnusEriksson it is not. – Tom Nov 18 '17 at 09:41
  • It is defined but I want to make it so the HTML code only outputs if results are given in the db – Tom Nov 18 '17 at 09:42
  • If your query doesn't produce any results, the $id (and the rest of the variables) won't be set but your code will still try to echo them. So, in some cases, you will try and use some variables that are undefined, which is what the dupe is talking about. – M. Eriksson Nov 18 '17 at 09:42
  • I get that and I want to make it so it won't echo them unless they are set... – Tom Nov 18 '17 at 09:43
  • Check if they are defined first. `if (isset($id)) { ...your html... }`. (Which the answer in the dupe _also_ mentions so, still a dupe). – M. Eriksson Nov 18 '17 at 09:45
  • Do you expect that query returns one row? – splash58 Nov 18 '17 at 09:46
  • Can you do a `print_r($result);` to show us your output of the execution? That would make your question more complete, and easier to answer. – ash Nov 18 '17 at 09:58

2 Answers2

1

set flag variable before while loop then use that variable to decide whether to print data in html or not

$data_exist = false;
if (mysqli_num_rows($result) > 0) {
    // output data of each row
    $data_exist = true; 
    while($row = mysqli_fetch_assoc($result)) {
      $id = $row["id"];
      $teacher_set = $row["teacher_set"];
      $class = $row["class"];
      $name = $row["name"];
      $description = $row["description"];

    }
}


if($data_exist)
{
?>

          <p><?php echo $id?></p>
          <p><?php echo $teacher_set?></p>
          <p><?php echo $class?></p>
          <p><?php echo $name?></p>
          <p><?php echo $description?></p>
<?php
} 
?>
B. Desai
  • 16,414
  • 5
  • 26
  • 47
0
 $sql = "SELECT * FROM homework WHERE class = '$class'";
 $result = mysqli_query($conn, $sql);

 if (mysqli_num_rows($result) > 0) {
 while($row = mysqli_fetch_assoc($result)) { 
 ?>  
    <p><?php echo $row["id"];?></p>
    <p><?php echo $row["teacher_set"];?></p>
    <p><?php echo $row["class"];?></p>
    <p><?php echo $row["name"];?></p>
    <p><?php echo $row["description"];?></p>
<?php } } ?>
Ritesh Khatri
  • 1,253
  • 13
  • 29