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

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

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
    duanli0119 2017-05-13 20:56
    已采纳

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

    strlen($row['firstName']);
    
    点赞 评论

相关推荐