2014-02-25 62 views
-2

我有一些代碼,如下所示。我希望有一些幫助將它改爲正常的PHP語句,而不是面向對象的。 PHP還在學習中不太好。將表格註冊限制爲4人註冊

<?php   
    $connection=mysqli_connect("host", "username", "password", "database"); 
    $sql="SELECT COUNT(*) from database_users"; 
    $stmt = $connection->prepare($sql); 
    $stmt->execute(); 
    $res = $stmt->get_result(); 
    $users = $res->fetch_array(MYSQLI_ASSOC); 

    if ($users['COUNT(*)'] < 4) { 
?> 

    // html goes here, outside of the php tags 

<?php 
    } else { 

     echo "Sorry, you have reached the user account limit."; 

    }; 
?> 

任何想法?

感謝, 查理

+2

出了什麼問題,你有什麼?以這種方式使用mysqli是首選。 –

+1

這是什麼OOP? – user3004356

+0

老兄,這是香草的PHP有OOP在這裏。我不認爲你可以做更簡單的,這;) – jycr753

回答

0

試試這個,你可以使用別名COUNT(*)

<?php   
    $connection=mysqli_connect("host", "username", "password", "database"); 
    $sql="SELECT COUNT(*) as cnt from database_users"; 
    $res = mysqli_query($connection, $sql); 
    $users = $result->fetch_assoc(); 
    if ($users['cnt'] < 4) { 
?> 

    // html goes here, outside of the php tags 

<?php 
    } else { 

     echo "Sorry, you have reached the user account limit."; 

    } 
?> 
+0

雖然的確他應該使用'數(*)',這不是他失敗的原因。你應該說你爲什麼忽略在你的響應中使用':: get_result()'來使它更有幫助^^ – Jon