1

Getting error

Notice: Undefined variable: row in C:\xampp\htdocs\ipo\details.php on line 65.

I am using php with sql.

<?php
    include 'header.php';
    include 'navbarmain.php';
    $count = 0;

    $PageID = (int) trim($_GET['id']);
    if($PageID == 0){
        //you can show error or redirect to other page
    }

    $mysqli = mysqli_query($connection,"SELECT * FROM products WHERE id='{$PageID}' LIMIT 1");
    foreach($mysqli as $row){
        $count++;
        if($count == 0) { // three items in a row
            $count = 0;
        }
    }

?>

<a href="#">
  <img class="media-object" src="<?php echo $row["filename"]; ?>" alt="...">
  <span class="nsbs">NSE / BSE</span>
</a>
mickmackusa
  • 43,625
  • 12
  • 83
  • 136
  • `LIMIT 1` will only give back one row, at most. Not sure what you mean by `three items in a row` nor the `foreach` loop, or the `$count`. – user3783243 Mar 06 '20 at 04:42
  • If `$mysqli` is not iterable (because of an error an empty result set) then `$row` will not be defined. If `id` is the primary key, then you don't need `LIMIT 1`. – mickmackusa Mar 06 '20 at 04:42
  • Either set a default value for when there is no row in the result set, or write your html inside of the `foreach(){}`. – mickmackusa Mar 06 '20 at 04:48

1 Answers1

-1

You are trying to write filename outside the loop .That might be the reason for your error. Please try below code

<?php
    include 'header.php';
    include 'navbarmain.php';
    $count = 0;

    $PageID = (int) trim($_GET['id']);
    if($PageID == 0){
        //you can show error or redirect to other page
    }

    $mysqli = mysqli_query($connection,"SELECT * FROM products WHERE id='{$PageID}' LIMIT 1");
    foreach($mysqli as $row){
        $count++;
        if($count == 0) { // three items in a row
            $count = 0;
        }

    ?>
    <a href="#">
      <img class="media-object" src="<?php echo $row["filename"]; ?>" alt="...">
      <span class="nsbs">NSE / BSE</span>
    </a>

     <?php
    }

?>
Lokesh Jain
  • 579
  • 6
  • 19