From what I have read, I think what you want to know is that mysql_fetch_array($result) returns an array if the mysql result had at least one row. It will return false if there was no row of data from the query. The first $row=mysql_fetch_array($result) assigns either false or an array of the data from the query to the variable $row. You could use that in the if() statement, but I don't know why it wouldn't work for you.
$query="select * from voter where lname='$lname' AND fname='$fname' AND dob='$dob'";
$result=mysql_query($query);
if($row=mysql_fetch_array($result)){
echo "Congratulations"." ". $row['fname']." Your date of birth is"." ". $row['dob']." and your registration number is ". $row['reg_num'];
}
else {
echo "<br /><p>","Your record was not found.<br /><br /> Please contact our office at xxx-xxx-xxxx for assistance.";
}
I use it that way quite a bit. It will evaluate the expression $row=mysql_fetch_array($result) and if it had a row, well then, $row would not equal false, making the statement true. It should therefore execute the code to display the information.
I don't know if defining the variable $row twice would cause problems. You can check out the way mysql_fetch_row() works at
php.net. Although most of the time I use the $result variable to check if the query was successful instead.
$query="select * from voter where lname='$lname' AND fname='$fname' AND dob='$dob'";
$result=mysql_query($query);
if($result){
$row=mysql_fetch_array($result);
echo "Congratulations"." ". $row['fname']." Your date of birth is"." ". $row['dob']." and your registration number is ". $row['reg_num'];
}
else {
echo "<br /><p>","Your record was not found.<br /><br /> Please contact our office at xxx-xxx-xxxx for assistance.";
}
I guess it all depends on how you feel about it. If you are focused on speed, then you would probably use the latter because if the query returned no results, then $result would be false, and mysql_fetch_array($result) would never be executed because there would be no point to fetch an array from an empty result set. It would return false too. For more info on mysql_query() check out
this at php.net.
If you are sure there is only one result this will work great for you. But if you need to do it for more than one result then the while($row=mysql_fetch_array($result)) will allow you to go through each row. You can make sure there is only one row if any matches occur by using the "limit 1" sql syntax. Basically change $query to $query="select * from voter where lname='$lname' AND fname='$fname' AND dob='$dob' LIMIT 1"; and only one row will exist regardless if the query matches multiple rows from the table.
Hope this helps you somehow.