So it has been a few days, and i still can't get it to work properly
On the website i got this in the code:
<?php
include ('function.php');
echo countUser('ko');
?>
And I'm using this function to count how many 'ko' there are. If there's more than 5, then it should say "There is no available space left" But if there's 2 'ko' then i want the ELSE to say "there are 3 spaces left"
So here's my code:
<?php
$con = mysql_connect("localhost","root");
mysql_select_db("cursussite");
function countUser ($cursus)
{
$sql = "SELECT COUNT(*) AS Totalcursus FROM cursisten WHERE cursus = '".$cursus."' ";
$query = mysql_query($sql) or die (mysql_error());
$data = mysql_fetch_row($query) or die (mysql_error());
if ($data > 5){
echo "Alle plaatsen zijn bezet.";
}
else {
echo "Er zijn nog " .$data. " plaatsen beschikbaar";
}
}
?>
I executed the code but i'm getting : Alle plaatsen zijn bezet. The weird thing is that I only have two records in the database with 'ko'.
What am I doing wrong?
You are counting the wrong variable. $data
will hold the current row fetched. Instead you should use this:
$data = mysql_fetch_assoc($query) or die (mysql_error());
$count = $data['Totalcursus'];
if ( $count > 5 ) {
// ...
And the obvious addition: mysql
extension is deprecated. Try migrating your code to mysqli
or PDO
.