dongxing2302 2017-05-11 13:43
浏览 216
已采纳

从数据库中获取的值的长度

I need to find how many character each first name has and arrange from higher to lower. This code will print the id first name and last name using data table, I want to show the number of character each has in separate table with the corresponding first name.

<!DOCTYPE html>
<html>
<head>
    <title></title>

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>    
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
    <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/v/dt/dt-1.10.15/datatables.min.css"/>
    <script type="text/javascript" src="https://cdn.datatables.net/1.10.15/js/jquery.dataTables.min.js"></script>
    <script type="text/javascript" src="https://cdn.datatables.net/1.10.15/js/dataTables.jqueryui.min.js"></script> 

</head>
<body>
    <?php
        $servername = "localhost";
        $username = "root";
        $password = "";
        $dbname = "name";

        $conn = new mysqli($servername, $username, $password, $dbname);

         if(mysqli_connect_errno($conn))
         {
            echo 'Failed to connect to database: '.mysqli_connect_error();
        }
        else{}
        $sql="SELECT * FROM nametable";
        $result=mysqli_query($conn, $sql);

        if ($result->num_rows > 0) {
            echo "<table width=50% class='dataTable'><thead><tr><th>id</th><th>Firstname</th><th>Lastname</th></tr></thead><tbody>";
         while($row = $result->fetch_assoc()) {
            echo "<tr><td>".$row["id"]."</td><td>".$row["firstName"]."</td><td>".$row["lastName"]."</td></tr>";
          }
          echo "</tbody></table>";
        }

    ?>
<script>
    $(document).ready(function() {
        $(".dataTable").DataTable({
            "lengthMenu": [[10, 25, 50, 100, -1], [10, 25, 50, 100, "All"]],
            "pagingType": "numbers"
        } );
    } );
</script>

<?php
        $sql1="SELECT firstName, LEN(firstName) as LengthOfFirstName FROM nametable";
        $result2=mysqli_query($conn, $sql1);

        if ($result2->num_rows > 0){
            while ($row2 = $result2->fetch_assoc()){
                    echo  $row2 ."<br>";
                }
            }
?>
</body>
</html>
  • 写回答

1条回答 默认 最新

  • duanli0119 2017-05-13 20:56
    关注

    You seem to be looking for strlen, which returns the length of the input string:

    strlen($row['firstName']);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)