Splycd
Splycd

Reputation: 161

Loop an If Statement for each row in Sql table

I am trying to make an If statement in PHP loop for each row in an Sql table. I think I'm almost there but there is a syntax error that I cant quite figure out. Heres what i've got so far:

$mysqli = new mysqli("localhost", "root", "usbw", "favourites");

                // check connection
                if ($mysqli->connect_errno) {
                    die("Connect failed: ".$mysqli->connect_error);
                }

                $query = "SELECT * FROM defaultTiles";
                $result = $mysqli->query($query);

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

                echo "<?php if ($tile_name === '$row[name]'){
                    $tile_colour = '$row[colour]';
                    $img_url = '$row[img_url]';
                    $link_url = '$row[link_url]';
                } ?>";
                }

Upvotes: 1

Views: 301

Answers (1)

Lal
Lal

Reputation: 14810

Replace

while($row = $result->fetch_array()){
     echo "<?php if ($tile_name === '$row[name]'){
                    $tile_colour = '$row[colour]';
                    $img_url = '$row[img_url]';
                    $link_url = '$row[link_url]';
     } ?>";
}

with

while($row = $result->fetch_array()){
     if ($tile_name === $row['name']){
          $tile_colour = $row['colour'];
          $img_url = $row['img_url'];
          $link_url = $row['link_url'];
     } 
}

as, when you put an echo in front of the if statement, it just prints the statement inside the echo.

Upvotes: 2

Related Questions