总字符数(LENGTH)

时间:2016-10-19 16:50:33

标签: php

我需要显示总共的标题字符。 $ totals只返回0,我哪里错了?

有什么建议吗?

    $characters_qty = mysql_query("
    SELECT LENGTH(title) AS title_quantity, title FROM orders 
    ");

    $res_characters_qty = mysql_fetch_assoc($characters_qty);
    $totals = $res_characters_qty ['title_quantity'];
    echo $totals;

1 个答案:

答案 0 :(得分:0)

请尝试使用至少使用mysqli的内容。测试了它和它的工作。

$servername = "localhost";
$username = "your_username";
$password = "your_password";
$dbname = "your_db_name";

$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 


    $sql = "SELECT LENGTH(title) AS title_quantity, title FROM orders";
    $res_characters_qty  = mysqli_query($conn, $sql);
    if (mysqli_num_rows($res_characters_qty ) > 0) {
        while($row = mysqli_fetch_assoc($res_characters_qty )) {
            echo $row['title_quantity']. '<br/>';

        }
}

$conn->close();