I am getting this error when my form is completed, despite the fact that the successful response is correctly displayed and the item is added to the database.
Here’s the error
Warning: mysql_free_result(): supplied argument is not a valid MySQL result resource in /home/site4/public_html/lab/mailinglist/mailing_list_external.php on line 18
Line 18 is
mysql_free_result($check_res);
and the entire PHP file is
<?php
include("mailing_list_include.php");
// determine if they need to see the form or not
if ($_POST["email"] == "") {
header("Location: mailing_list_external.php");
exit;
} else {
// connect to database
doDB();
// check that the email is in list
emailChecker($_POST["email"]);
// get number of results and do action
if (mysqli_num_rows($check_res) < 1) {
// free result
mysql_free_result($check_res);
// add record
$add_sql = "INSERT INTO subscribers (email)
VALUES('".$_POST["email"]."')";
$add_res = mysqli_query($mysqli, $add_sql)
or die(mysqli_error($mysqli));
$display_block = "<p>Thanks for signing up!</p>";
// close connection to mysql
mysqli_close($mysqli);
} else {
// print failure message
$display_block = "<p>You're already subscribed!</p>";
}
}
?>
<html>
<body>
<?php echo "$display_block"; ?>
</body>
</html>
Like I said, it works fine but I still get this error…
Here’s the code for the included file (mailing_list_include.php)
<?php
function doDB() {
global $mysqli;
// connect to server and select database; you may need it
$mysqli = mysqli_connect("localhost", "XXX",
"XXX", "XXX");
// if connection fails, stop script execution
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
}
function emailChecker($email) {
global $mysqli, $check_res;
//check that email is not already in list
$check_sql = "SELECT id FROM subscribers
WHERE email = '".$email."'";
$check_res = mysqli_query($mysqli, $check_sql)
or die(mysqli_error($mysqli));
}
?>
Use mysqli_free_result – I suspect it’s because you’re using mysql_free_result, which is a different library