if语句不适用于db查询

时间:2014-09-27 14:27:26

标签: php database if-statement

我正在运行下面的代码,但无法使if语句生效。我希望能够在没有内容的情况下删除div,如果有则显示它,但是如果一个db条目为空而不是仅删除1个空行,则它将全部删除。

             <?php 
     $con = new mysqli($DBServer, $DBUser, $DBPass, $DBName);
     if (mysqli_connect_errno()) {
     echo "Failed to connect to MySQL: " . mysqli_connect_error();
     }
     extract($_GET);
     { $result = mysqli_query($con,"SELECT * FROM links ORDER by id ");
     while($row = mysqli_fetch_array($result)) {

     if ($section=""){
     echo"";
     }
     elseif ($section="a"){
     echo"<div class=\"page-header\">" . $row['section'] . "\</div>";
     }


      echo "<div class=\"links-wrap\">
         <div class=\"links-box\">
         <div class=\"links-image\"><img src=\"images/links/" . $row['img1'] . "\"></div>
         <div class=\"links-site-txt\">" . $row['link1'] . "</div>
         <div class=\"links-desc\">" . $row['desc1'] . "</div>
         </div>
         <div class=\"spacer-left\"></div>
         <div class=\"links-box\"></div>
         <div class=\"spacer-left\"></div>
         <div class=\"links-box\"></div>
          </div>";}}
      mysqli_close($con);
       ?>  

2 个答案:

答案 0 :(得分:0)

要比ifelseif ==比较值,而不是==通常用于为变量赋值。如下更改您的代码。

$con = new mysqli($DBServer, $DBUser, $DBPass, $DBName);
if (mysqli_connect_errno()) {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
extract($_GET);

$result = mysqli_query($con,"SELECT * FROM links ORDER by id ");

while($row = mysqli_fetch_array($result)) {
    $section = $row['section'];
    if ($section == ""){
        echo "";
    }
    elseif ($section == "a"){
        echo"<div class=\"page-header\">" . $row['section'] . "\</div>";
    }

    echo "<div class=\"links-wrap\">
     <div class=\"links-box\">
     <div class=\"links-image\"><img src=\"images/links/" . $row['img1'] . "\"></div>
     <div class=\"links-site-txt\">" . $row['link1'] . "</div>
     <div class=\"links-desc\">" . $row['desc1'] . "</div>
     </div>
     <div class=\"spacer-left\"></div>
     <div class=\"links-box\"></div>
     <div class=\"spacer-left\"></div>
     <div class=\"links-box\"></div>
      </div>";
}

mysqli_close($con);

答案 1 :(得分:0)

<?php 
 $con = new mysqli($DBServer, $DBUser, $DBPass, $DBName);
 if (mysqli_connect_errno()) {
 echo "Failed to connect to MySQL: " . mysqli_connect_error();
 }
 extract($_GET);
 { $result = mysqli_query($con,"SELECT * FROM links ORDER by id ");
 while($row = mysqli_fetch_array($result)) {

 if ( $section == "" ){ //double equality for condition check
 echo "";
 }
 else if ($section == "a" ){
 echo"<div class=\"page-header\">" . $row['section'] . "\</div>";
 }


  echo "<div class=\"links-wrap\">
     <div class=\"links-box\">
     <div class=\"links-image\"><img src=\"images/links/" . $row['img1'] . "\"></div>
     <div class=\"links-site-txt\">" . $row['link1'] . "</div>
     <div class=\"links-desc\">" . $row['desc1'] . "</div>
     </div>
     <div class=\"spacer-left\"></div>
     <div class=\"links-box\"></div>
     <div class=\"spacer-left\"></div>
     <div class=\"links-box\"></div>
      </div>";}}
  mysqli_close($con);
   ?>