I'm getting this error in my PHP script but I can't see anything wrong with it:
PHP Warning: mysqli_affected_rows() expects parameter 1 to be mysqli, boolean given in C:\Webpages\hws\admin\add_class_students_rows.php on line 83
Here is part of my script. It basically emails everyone in the array called $emails using the mail function. Before it does this it adds a row to the database.
As far as I know, if the query is okay and it is an insert, the result from mysql_query returns true. If it is an insert you use mysql_affected_rows (as I have) otherwise you use mysql_num_rows instead. I've used code like this before and there's been no problem, I've looked up the function in the php manual and tried looking up the error, but help is thin on the ground.
I don't want anyone to come along and tell me to completely re-write my entire script, but if someone could please tell me why this is going wrong that would be a great help.
//Add the students to the system.
foreach($emails as $key => $value)
{
echo $activationQuery = "insert into students(activationCode,activated,email) values(".$activationCodes[$key].",0,\"".$emails[$key]."\")";
$activationResult=mysqli_query($mysqlcon,$activationQuery);
if($activationResult)
{
$rows=mysqli_affected_rows($activationResult);
//Check to see whether exactly one row was added.
if($rows!=1)
{
//Rolling back will prevent any bad row being added, although no rows were likely to have been.
mysqli_rollback($mysqlcon);
array_push($message,"Adding the user to the database with the email address ".$emails[$key]." failed.");
}
else
{
//Commit because there are no problems.
mysqli_commit($mysqlcon);
}
}
else
{
//Rolling back will prevent any bad row being added, although no rows were likely to have been.
mysqli_rollback($mysqlcon);
//If this is true then the query failed.
array_push($message,"The insert query into the database failed.");
}
}//end foreach
//free up memory associated with the query.
mysqli_free_result($activationResult);
Mail Function
Also, when using the mail function, do I have to set the sendmail_from inside the php.ini? Do I have to send it from a proper mail account set up on my computer or will it send without?
Here is my php.ini at the moment:
[mail function]
; For Win32 only.
SMTP = localhost
smtp_port = 25
; For Win32 only.
sendmail_from = danielcottrell@operamail.com
; For Unix only. You may supply arguments as well (default: "sendmail -t -i").
;sendmail_path =
; Force the addition of the specified parameters to be passed as extra parameters
; to the sendmail binary. These parameters will always replace the value of
; the 5th parameter to mail(), even in safe mode.
;mail.force_extra_parameters =