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

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

Who is Participating?
chaddcarrAuthor Commented:
//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'];
You need to get the count of $result_max like you do with $result_cnt
Ray PaseurCommented:
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.
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

chaddcarrAuthor Commented:
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.
Very first comment tells you you need to get the recordset count and not compare the a number against a recordset.
Ray PaseurCommented:
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!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.