Click here to Skip to main content
15,911,141 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Helo Coders

I'm trying to insert a record into a database table but i'm getting this error

mysql_num_rows() expects parameter 1 to be resource, boolean given in C:\wamp\www\United_Nations\register.php on line 107

I do not know what is wrong. Please help

include('localhost.php');
if (isset($_POST["SubmitButton"])){
$fullname=$_POST['FirstName'];
$Gender=$_POST['YourGender'];
$AgeGroup=$_POST['AgeGroup'];
$Organisation=$_POST['Org'];
$Purpose=$_POST['Purpose'];
$NRC=$_POST['NRC'];
$Province=$_POST['Province'];
$dbDate=$_POST['dbDate'];
$Address=$_POST['Address'];
$query=mysql_query("SELECT * FROM students WHERE NRC='$NRC'");
$numrows=mysql_num_rows($query); // this is line 107 in my editor
if($numrows==0){
$sql = ("INSERT INTO `students` set FullName='$fullname', Gender='$Gender', AgeGroup='$AgeGroup', Organisation='$Organisation', Purpose='$Purpose', NRC='$NRC', Province='$Province', dbDate='$dbDate', Address='$Address'");
$result=mysql_query($sql);
if($result)
echo " Data has been Saved ";
}else {
echo "Could not Save Data";
}
}
?>
Posted

mysql_query returned false, that caused the error at mysql_num_rows(), you could have concatenated using . in php like this:
WHERE NRC='".$NRC."'");

However, there are two reasons here that you should not do it, rewrite your code instead:
1. mysql extension has been deprecated as of PHP 5.5, it is recommended to use mysqli or PDO, read more: Choosing MySQL APIs[^]
2. Use prepared statement instead of injecting parameters directly in the sql query owing to sql injection risk, read more: Prepared Statements in PHP and MySQLi[^]
 
Share this answer
 
v2
Check $query , or $sql before passing it to mysql_fetch_array or mysql_num_rows . You'll find that it's false because the query failed
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900