Limit registration to 4 people on form sign up [du

2019-09-21 12:04发布

问题:

This question already has an answer here:

  • Call to undefined method mysqli_stmt::get_result 10 answers

I have some code, seen below. I would like some help changing this into normal PHP statement and not object orientated. Not very good with PHP still learning.

<?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.";

    };
?> 

Any ideas?

Thanks, Charlie

回答1:

try this you can use an alias for 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.";

    }
?>