-2

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

  • 2
    What's wrong with what you have? Using mysqli in this way is preferred. – Jonathon Reinhart Feb 25 '14 at 09:03
  • Dude this is vanilla php there is OOP here. i dont think you can do more simple that this ;) – jycr753 Feb 25 '14 at 09:05
  • Hi @JonathonReinhart It has come up with an error, and I'm not used to using object orientated. It works on my local host but comes up with Fatal error: Call to undefined method mysqli_stmt::get_result() in /home/.../public_html/.../users.php on line 119 – CharlieW95 Feb 25 '14 at 09:05
  • @user3004356 He's probably referring to the OOP mysqli calls, like `$stmt->execute()`. – Jonathon Reinhart Feb 25 '14 at 09:05
  • What version of PHP are you using? [`mysqli_stmt::get_result`](http://www.php.net/manual/en/mysqli-stmt.get-result.php) is only available on PHP >= 5.3.0. – Jonathon Reinhart Feb 25 '14 at 09:07
  • PHP version 5.3.27 @JonathonReinhart – CharlieW95 Feb 25 '14 at 09:09
  • 1
    It will help in the future to point out the specific problem you're having, as opposed to asking for general advice on how to do something. Here, searching for that error message leads to tons of advice. – Jonathon Reinhart Feb 25 '14 at 09:10
  • How you are trying to access `$users` will give an undefined index error as well, as it won't have the index of `count(*)`. Instead, in the query, you'd want to use `SELECT count(*) as something FROM...` and access it with `$users['something']` – Jon Feb 25 '14 at 09:12

1 Answers1

0

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

    }
?>
Satish Sharma
  • 9,217
  • 6
  • 24
  • 49
  • While it's true that he should use an alias for `count(*)`, that isn't the reason it's failing for him. You should say why you neglected to use `::get_result()` in your response to make it more helpful ^^ – Jon Feb 25 '14 at 09:18