我正在尝试在PHP循环中为Sql表中的每一行创建一个If语句。我想我差不多了但是有一个语法错误,我无法弄明白。这是我到目前为止所得到的:
$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]';
} ?>";
}
答案 0 :(得分:2)
替换
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]';
} ?>";
}
与
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,当你在echo
语句前加上if
时,它只是在echo
内打印语句。