代码之家  ›  专栏  ›  技术社区  ›  Max

concat()函数或MySQL查询中的替代解决方案

  •  0
  • Max  · 技术社区  · 7 年前

    我正在尝试在MySQL查询中添加一个字符before/after值。但我做不到。 在我的情况下,这是不起作用的部分:

    $query = "select CONCAT ('.', DuRpt) as DuRpt, DaRpt from DDtb order by DATE DESC"; 
    

    您可以看到下面的完整代码。请告诉我为什么它不起作用,或者我能找到另一种解决办法吗?谢谢。

    <div class="container">
    <div class="left">
    <?php
    
    include ("etc/config.php");
    
    $query = "select concat ('.', DuRpt) as DuRpt, DaRpt from DDtb order by DATE DESC"; 
    $result = mysqli_query($link, $query); 
    if (!$result) { 
        $message = 'ERROR:' . mysqli_error($link); 
        return $message; 
    } else { 
        $i = 0; 
        echo '<form name="select" action="" method="GET">';
        echo '<select name="mySelect" id="mySelect"  size="44" onchange="this.form.submit()">'; 
        while ($i < mysqli_field_count($link)) { 
            $meta =  
                mysqli_fetch_field_direct($result, $i); 
            echo '<option>' . $meta->name . '</option>'; 
            $i = $i + 1; 
        } 
        echo '</select>';
        echo '</form>';
    }
    
        ?>
    
       </div>
      <div>
    
      <?php
    
    if(isset($_GET['mySelect'])) {
        $myselect = $_GET['mySelect']; 
        $sql = "SELECT `$myselect` as mySelect from DDtb order by DATE DESC";   
        $result = mysqli_query($link, $sql);   
    
        if ($result->num_rows > 0) {
    
            $table_row_counter = 3;
            echo '<table>';
    
            while($row = $result->fetch_assoc()) 
            {
                $table_row_counter++;
                if ($table_row_counter % 30 == 1) {
                    echo '</table>';
                    echo '<table>';
                }
    
                echo "<tr><td>" . $row["mySelect"] . "</td></tr>";
    
            }
        }
    } 
    
    echo '</table>';
    mysqli_close($link);
    ?> 
    
    </div>
    </div>
    
    2 回复  |  直到 7 年前
        1
  •  1
  •   khartnett    7 年前

    if(isset($_GET['mySelect'])) {
        // configure every option here, if there's not pre/postfix, use a blank string
        $prepostfixes = [
            'DuRpt' => ['.', '.'],
            'DaRpt' => ['', ''],
        ];
        $myselect = $_GET['mySelect'];
        if (!isset($prepostfixes[$myselect])) {
            die ('Unknown Select'); // this will prevent sql injection
        }
        $sql = "SELECT `$myselect` as mySelect from DDtb order by DATE DESC";
        $result = mysqli_query($link, $sql);
    
        if ($result->num_rows > 0) {
    
            $table_row_counter = 3;
            echo '<table>';
            $prefix = $prepostfixes[$myselect][0];
            $postfix = $prepostfixes[$myselect][1];
    
            while($row = $result->fetch_assoc())
            {
                $table_row_counter++;
                if ($table_row_counter % 30 == 1) {
                    echo '</table>';
                    echo '<table>';
                }
    
                echo "<tr><td>" . $prefix . $row["mySelect"] . $postfix . "</td></tr>";
    
            }
        }
    }
    
        2
  •  0
  •   user2203703    7 年前

    DuRpt

    $query = "select concat ('.', DuRpt) as DuRpt from DDtb order by DATE DESC";