【问题标题】:How to echo If statement from database如何从数据库中回显 If 语句
【发布时间】:2016-05-15 00:23:35
【问题描述】:

我在 while 循环内的 IF 语句中显示信息时遇到问题。甚至可以在 while 循环中回显 if 语句吗?请帮忙!

这段代码

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "databasename";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection

$searchEscaped = $conn->real_escape_string($_GET['username']);
if ($conn->connect_error) {
     die("Connection failed: " . $conn->connect_error);
} 

$sql = "SELECT * FROM users WHERE username = '$searchEscaped' ";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
     // output data of each row
     while($row = $result->fetch_assoc()) {
      echo "
    **if(!empty($row['image2'])) { 
    <a class='example-image-link' href='pictures/".$row['image2']."' data-lightbox='example-set'><img class='example-image'src='pictures/".$row['image2']."'  alt='Profile Pic'></a>
     }
      ";}
} else {
     echo "No users found";
}

$conn->close();
?>  

【问题讨论】:

    标签: javascript php mysql if-statement mysqli


    【解决方案1】:

    我认为你需要在 if 语句中移动你的 echo 以仅在满足要求时才回显:

    <?php
    $servername = "localhost";
    $username = "root";
    $password = "";
    $dbname = "databasename";
    
    // Create connection
    $conn = new mysqli($servername, $username, $password, $dbname);
    // Check connection
    
    $searchEscaped = $conn->real_escape_string($_GET['username']);
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    }
    
    $sql = "SELECT * FROM users WHERE username = '$searchEscaped' ";
    $result = $conn->query($sql);
    
    if ($result->num_rows > 0) {
        // output data of each row
        while ($row = $result->fetch_assoc()) {
    
            if (!empty($row['image2'])) {
                echo "
        <a class='example-image-link' href='pictures/" . $row['image2'] . "' data-lightbox='example-set'><img class='example-image'src='pictures/" . $row['image2'] . "'  alt='Profile Pic'></a>
    
          ";
            }
        }
    } else {
        echo "No users found";
    }
    
    $conn->close();
    ?>  
    

    下面是一个可在整个脚本中使用的函数示例:

        function print_image($row_image){
            if(!empty($row_image)){
                return  "
            <a class='example-image-link' href='pictures/" . $row_image . "' data-lightbox='example-set'><img class='example-image'src='pictures/" . $row_image . "'  alt='Profile Pic'></a>
    
              ";
            }
    else{
            return "";
        }
        }
    

    您可以在脚本中的任何其他位置调用此函数并执行以下操作:

    echo print_image($row['image2']);

    echo print_image($row['image3']);

    【讨论】:

    • 谢谢!之后我还有其他具有相同 if 语句的图像,所以我想呼应整个语句。
    • @kenny 我认为在这种情况下最好的解决方案是创建一个函数以避免重复您的代码。
    • @kenny 我做了一些补充,可能会有所帮助。
    【解决方案2】:

    if 语句在 echo 之前,if 语句 echo。

    if ($result->num_rows > 0)
    {
        // output data of each row
        while($row = $result->fetch_assoc())
        {
            if( ! empty( $row['image2'] ) )
            { 
                echo '<a class="example-image-link" href="pictures/' . $row['image2'] . '" data-lightbox="example-set"><img class="example-image" src="pictures/' . $row['image2']. '"  alt="Profile Pic"></a>';
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2013-07-05
      • 2023-02-26
      • 2015-02-09
      • 1970-01-01
      • 2021-12-07
      • 1970-01-01
      • 2013-04-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多