Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2014-08-23
7
Medium Priority
?
260 Views
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:

users
user_id

company_information
max_records

Here is my code so far.  It does not produce anything.
<?php
$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.");
    exit();
} 

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

?>

Open in new window

0
Comment
Question by:chaddcarr
  • 2
  • 2
  • 2
6 Comments
 
LVL 58

Assisted Solution

by:Gary
Gary earned 2000 total points
ID: 40280924
You need to get the count of $result_max like you do with $result_cnt
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 40281001
This article teaches how to use PHP and MySQLi.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/PHP_Databases/A_11177-PHP-MySQL-Deprecated-as-of-PHP-5-5-0.html

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.
0
 

Accepted Solution

by:
chaddcarr earned 0 total points
ID: 40293941
//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'];
0
Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

 

Author Comment

by:chaddcarr
ID: 40293961
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.
0
 
LVL 58

Expert Comment

by:Gary
ID: 40293962
Very first comment tells you you need to get the recordset count and not compare the a number against a recordset.
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 40293995
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.  
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_11769-And-by-the-way-I-am-new-to-PHP.html

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

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses
Course of the Month21 days, 6 hours left to enroll

810 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