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']);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化