Robert Luyt
Robert Luyt

Reputation: 399

check a SQL query with if / else in PHP

I'm a complete novice in this but try to learn a lot by myself / tutorials. However I can not seem to find the answer to this (probably) easy problem. I want to check an output from my SQL database if it is smaller than 10 and then give me the current value of the INT. If it is not yet 10 or more then I want to output a line and if so output an other line. However, it does not seem to work properly at the moment.

//Connect to DB
$con = mysqli_connect("localhost","root","root","cursuson");
//Get counter!
$getcounter = mysqli_query($con,"SELECT * FROM cursus01");
$cursus01 = mysqli_fetch_assoc($getcounter);

//check if output is less than 10
if(count($cursus01) <= 10)
    echo 'aantal aanmeldingen is ' . $cursus01['counter'] . '<br>';
else 
    echo 'De cursus gaat door! <br>';

//check the output
echo '<b>Aantal aanmeldingen:</b> ' . $cursus01['counter'] . '<br>'; 
echo '<b>ID:</b> ' . $cursus01['id'];

Upvotes: 2

Views: 16896

Answers (4)

nurakantech
nurakantech

Reputation: 502

I assume you want to count the number of records stored in the table. use mysqli_num_rows() function instead of count().

$con = mysqli_connect("localhost","root","root","cursuson");
$getcounter = mysqli_query($con,"SELECT * FROM cursus01");
$row = array();//create an empty array
//check if output is less than 10
if(mysqli_num_rows($getCounter) <= 10):
  $row = mysqli_fetch_array($getCounter);
  echo $row['counter']."<br>";
else: 
 echo 'De cursus gaat door! <br>';
endif;

//display the output
echo '<b>Aantal aanmeldingen:</b> ' . $row['counter'] . '<br>'; 
echo '<b>ID:</b> ' . $row['id'];

Upvotes: -1

Steven
Steven

Reputation: 6148

I assumed that you were trying to get the count of rows from the database (as others have done) and have left the answer for if that were the case below this one. You might find it useful and it seems like a waste to delete it!

Anyway, judging from your comment above and the $cursus01['counter'] in your question I'm now going to assume that what you actually want is the following...

Solution

I suggest that you use the mysqli method below as mysql will at some point in the near future be removed from the language as it is deprecated! If you continue to use it and create sites/apps with it then when this eventually happens you'll either have to use an old version of php which has it's own risks/limitations attached or face having a lot of broken code to fix...

mysql

mysql_connect("localhost","root","root");
mysql_select_db("cursuson");

$query = mysql_query("SELECT * FROM cursus01");

if(mysql_num_rows($query)){
    //Rows are returned
    while($row = mysql_fetch_assoc($query)){
        //Do stuff with row data
        if($row['counter'] <= 10){
            //Counter is less than or exactly 10
        }
        else{
            //Counter is greater than 10
        }
    }
}
else{
    //No rows were returned
}

mysqli

$mysqli = new mysqli('localhost', 'root', 'root', 'cursuson');

$result = $mysqli->query("SELECT * FROM cursus01"); 

if($result->num_rows;){
    //Rows are returned
    while($row = $result->fetch_assoc()){
        //Do stuff with row data
        if($row['counter'] <= 10){
            //Counter is less than or exactly 10
        }
        else{
            //Counter is greater than 10
        }
    }
}
else{
    //No rows were returned
}



Original answer

I assume that you are actually trying to echo a count of the number of rows returned? Your current code echos a count of the number of coulmns. If you don't know already it would suggest that there are bigger problems here....

Solution

mysql

mysql_connect("localhost","root","root");
mysql_select_db("cursuson");

$query = mysql_query("SELECT * FROM cursus01");
$count = mysql_num_rows($query);

if($count <= 10){
    //Less than (or exactly) 10 rows were returned
    while($row = mysql_fetch_assoc($query)){
        //Do stuff with row data
    }
}
else if($count > 10){
    //More than 10 rows were returned
    while($row = mysql_fetch_assoc($query)){
        //Do stuff with row data
    }
}
else{
    //No rows were returned
}

mysqli

$mysqli = new mysqli('localhost', 'root', 'root', 'cursuson');

$result = $mysqli->query("SELECT * FROM cursus01");
$count  = $query->num_rows;

if($count <= 10){
    //Less than (or exactly) 10 rows were returned
    while($row = $result->fetch_assoc()){
        //Do stuff with row data
    }
}
else if($count > 10){
    //More than 10 rows were returned
    while($row = $result->fetch_assoc()){
        //Do stuff with row data
    }
}
else{
    //No rows were returned
}

Upvotes: 2

Lolito
Lolito

Reputation: 412

Agreed with Bryan answer, but when doing counts of database rows I'd choose the option of using MySQL count. In this case code line would be:

$getcounter = mysql_query("SELECT COUNT(*) FROM cursus01");

You will get the exact number of rows using this query

Upvotes: 0

Bryan
Bryan

Reputation: 3494

The way you're doing this, the count is going to be the same every time. You're count($cursus01) returns the number of columns in your database, to get the number of rows you should use the num_rows function or do a count query, unless you need the data from the query in which case you should do something like....

$cursus01 = array();
while($row = mysql_fetch_assoc($getcounter))
    $cursus01[] = $row;

In this case count($cursus01) will return the number of rows. Hope i didn't confuse what you were asking. As edward pointed out you should start using mysqli instead of mysql. You also might want to look into using oop instead of procedural calls. see the manual for an example HERE

Upvotes: 0

Related Questions