PHP/MySQLi: How can I compare Operators in IF statement using variables from two tables

Posted on 2014-08-23
Last Modified: 2014-08-29
I have a basic <form> wherein administrators can add users into the database.  I would like to add an IF statement prior to the <form> so that IF the current number of uses has not exceeded the maximum number of users allowed for that account, the form will be displayed (and show how many users are remaining).  Conversely, IF the maximum has been exceed, print/echo a message to the admin stating the maximum has been exceeded and to request more (in place of the form).

There are two tables in use:



Here is my code so far.  It does not produce anything.
$result_max = mysqli_query("SELECT max_records FROM company_information"); 

$result_cnt = mysqli_query("SELECT user_id FROM users"); 

    /* determine number of rows result set */
    $row_cnt = mysqli_num_rows($result_cnt);

if ($row_cnt > $result_max) {
    printf("You have exceeded the maximum number of users for this account. Please request more.");

if ($row_cnt < $result_max) {
    echo "The html form will go here";


Open in new window

Question by:chaddcarr
    LVL 58

    Assisted Solution

    You need to get the count of $result_max like you do with $result_cnt
    LVL 107

    Expert Comment

    by:Ray Paseur
    This article teaches how to use PHP and MySQLi.

    Some of the things you need to add include error checking.  The MySQLi_Query() function has a return value.  If your script does not test this value, you cannot know whether the function succeeded or failed!  Fortunately all of the PHP functions are documented in the online man pages, so when you don't know what a function is expected to return, or when you're getting unexpected results, you can look them up online.  No need to guess ever again!

    I'm not sure how your data tables are organized or loaded, but if you want to show us the test data you're using we can probably show you a complete and working code example.  Or you can just refer to the article to see error checking and counting the rows in the results set.  You might also want to learn about SELECT COUNT(*) which can be used with a WHERE clause.

    Accepted Solution

    //retrieve current user count
    $count_user_sql = "SELECT COUNT(*) as user_count FROM users";
    $count_user_result = mysql_query($count_user_sql);
    $count_user_row = mysql_fetch_array($count_user_result);

    $current_user_count = $count_user_row["user_count"];

    //retrieve max_users
    $sql = "SELECT max_records FROM company_information WHERE company_id=1";
    $result = mysql_query($sql);
    $row = mysql_fetch_array($result);
    $max_users = $row['max_records'];

    Author Comment

    I've requested that this question be closed as follows:

    Accepted answer: 0 points for chaddcarr's comment #a40293941

    for the following reason:

    Comments provided by other users provided no assistance.
    LVL 58

    Expert Comment

    Very first comment tells you you need to get the recordset count and not compare the a number against a recordset.
    LVL 107

    Expert Comment

    by:Ray Paseur
    Agree with Gary's  Expert Comment.  The code snippet in the question evinces a complete lack of understanding about how to use PHP and MySQL together.  There's nothing wrong with ignorance -- most of us started somewhere else a long time ago.  But there is a learning process that we had to go through to build up enough of a knowledge base to know whether the comments provided any assistance or not.  

    If you want to get started learning PHP and MySQL, this article can help you get that knowledge base.

    Don't demand too much of yourself all at once -- most of us have been doing this for years!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
    Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
    The viewer will learn how to count occurrences of each item in an array.
    The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now