Forum Moderators: coopster

Message Too Old, No Replies

Warning: mysqli close() expects parameter 1 to be mysqli

         

Amy Ra Ra Ra

8:22 pm on Jun 14, 2012 (gmt 0)

10+ Year Member



/*I wrote the below php code and got an error when I clicked on the submit button. I should have gotten just the warning in echo when submitting, but I got an error too when I submitted the form. The error that I get in the window once I click on the submit button:

Warning: mysqli_close() expects parameter 1 to be mysqli, null given in /home/content/98/7731198/html/mygiftpackage/sendemail.php on line 39

*/

//The full code that I used for my php file:

<?php
$from = 'contact@doyouknow.com';
$subject = $_POST['subject'];
$text = $_POST['emailmessagefield'];

if ((!empty($subject))&& (!empty($text))){

$dbc = mysqli_connect('hostlocation.db.7731198.hostedresource.com', 'username', 'password', 'databasename')
or die('Error connecting to MySQL server.');

$query = "SELECT * FROM email_list_amy";

$result = mysqli_query($dbc, $query)
or die('Error querying database.');

while ($row = mysqli_fetch_array($result)){
$to = $row['email'];
$first_name = $row['first_name'];
$last_name = $row['last_name'];
$msg = "Dear $first_name $last_name,\n$text";
mail($to, $subject, $msg, 'From:' . $from);
echo "Email sent to: $to <br />";
}
mysqli_close ($dbc);
}
?>

/*How could the mysqli_close tag be wrong? Does anyone know what I should do? */

rocknbil

4:28 pm on Jun 15, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Huh. That should work, it must be already closing after the query. Shortest answer, since it says "null given" you can just do

if ($dbc) { mysqli_close ($dbc); }

This is a good approach to test a variable's existence without generating undefined variable errors in both PHP and mySQL. We don't need to know it's value(s), just if it exists or not.

Amy Ra Ra Ra

2:18 am on Jun 17, 2012 (gmt 0)

10+ Year Member



Rocknbil it didn't help. I tried the code but it failed.

coopster

11:39 pm on Jun 18, 2012 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



It's not the close that is failing then, more than likely the connection. Try doing a var_dump on the $dbc variable after attempting your connection to see what you get.

Amy Ra Ra Ra

12:11 am on Jun 20, 2012 (gmt 0)

10+ Year Member



I fixed the code. The problems was that the form field name was 'text' but I used 'emailmessagefield' instead, therefore it couldn't store the info from the form correctly. When I changed 'emailmessagefield' to 'text' the information in the if statement processed correctly.

Amy Ra Ra Ra

12:54 am on Jun 20, 2012 (gmt 0)

10+ Year Member



<!--The correct php file (let's pretend that I named my php file "sendemailtoregisteredusers.php") looks like this:
-->

<?php
$from = 'contact@mywebsite.com';
$subject = $_POST['subject'];
$text = $_POST['text'];

if ((!empty($subject))&&(!empty($text))){

$dbc = mysqli_connect('hostlocation', 'username', 'password', 'databasename')
or die('Error connecting to MySQL server.');

$query = "SELECT * FROM email_list_amy";
$result = mysqli_query($dbc, $query)
or die('Error querying database.');

while ($row = mysqli_fetch_array($result)){
$to = $row['email'];
$first_name = $row['first_name'];
$last_name = $row['last_name'];
$msg = "Dear $first_name $last_name,\n$text";
mail($to, $subject, $msg, 'From: ' . $from);
echo 'Email sent to: ' . $to . '<br />';
}

mysqli_close ($dbc);
}
else if ((empty($subject))&&(empty($text))){
echo 'You forgot the email subject and body text.<br />';
}
else if ((!empty($subject))&&(empty($text))){
echo "You forgot to fill in your message. <br />";
}
else if ((empty($subject))&&(!empty($text))){
echo "You forgot to fill in the subject field. <br />";
}
?>

THE HTML FORM CODE LOOKS LIKE THIS:

<div>
<form method="post" action="sendemailtoregisteredusers.php">
<label for="subject">Subject of email </label><br />
<input type="text" id="subject" size="60" name="subject" />
<br />
<br />
<label for="text">Body of email:</label><br />
<textarea id="text" name="text" rows="8" cols="60"></textarea>
<br />
<br />
<br />
<input type="submit" value="submit" name="submit" />
</form>
</div>