PHP读取数据库表长度

时间:2015-07-27 02:32:27

标签: php database

我有阅读表格长度的问题,

我想要像这样: -

if length < 10

    echo "length is less than 10"

else

    echo "length is more than 10"

这是我的代码

$sql1= mysql_query("SELECT * FROM `account` WHERE `username` = '$username'");
if(mysql_fetch_lengths($sql1) < 10) {
    echo ("length less than 10");
    exit();
} else {
    echo ("length more than 10");
    exit();
}

任何帮助?

2 个答案:

答案 0 :(得分:1)

另一种方式是:

<?php           
    $query = "SELECT COUNT(*) AS num FROM `account` WHERE `username` = '$username'";
    $sql1= mysqli_query($conn, $query);

    if( ! $sql1 ) {
        //query ok      
        $row = mysqli_fetch_assoc( $sql1 );                     
        if( $row['num'] < 10 ) {
            $msg = "length less than 10";
        }else {
            $msg = "length more than 10";
        }
        echo $msg;              
    }       
    exit();            
?>

答案 1 :(得分:0)

我认为你想使用mysql_num_rows()

$sql1= mysql_query("SELECT * FROM `account` WHERE `username` = '$username'");
if(mysql_num_rows($sql1) < 10) {
    echo ("length less than 10");
    exit();
} else {
    echo ("length more than 10");
    exit();
}