Hi all,

Im busy moving a site from localhost to hosted server and im baffled as to why mysqli simply wont run.

The following script iscalled after a captcha script has been satisfied, but it just doesnt seem to like the Mysqli. I have converted other simple mysqli scripts, but this one is giving me a big headache.

Is Mysqli something that can be activated in .htaccess?

Any help is appreciated.

Thanks

<?php
//set up a couple of functions
function doDB() {
	global $mysqli;

	//connect to server and select database; you may need it
	$mysqli = mysqli_connect("localhost", "------", "---------", "--------");

	//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));
}

//determine if they need to see the form or not
if (!$_POST) {
	//they need to see the form, so create form block
	$display_block = "
	<form method=\"POST\" action=\"".$_SERVER["PHP_SELF"]."\">

	<p><strong>Your E-Mail Address:</strong><br/>
	<input type=\"text\" name=\"email\" size=\"30\" maxlength=\"150\">

	<p><strong>Action:</strong><br/>
	<input type=\"radio\" name=\"action\" value=\"sub\" checked> subscribe
	<input type=\"radio\" name=\"action\" value=\"unsub\"> unsubscribe

	<p><input type=\"submit\" name=\"submit\" value=\"Submit Form\"></p>
	</form>";

} else if (($_POST) && ($_POST["action"] == "sub")) {
	//trying to subscribe; validate email address
	if ($_POST["email"] == "") {
		header("Location: manage.php");
		exit;
	} else {
		//connect to database
		doDB();

		//check that email is in list
		emailChecker($_POST["email"]);

		//get number of results and do action
		if (mysqli_num_rows($check_res) < 1) {
			//free result
			mysqli_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>";
		}
	}
} else if (($_POST) && ($_POST["action"] == "unsub")) {
	//trying to unsubscribe; validate email address
	if ($_POST["email"] == "") {
		header("Location: manage.php");
		exit;
	} else {
		//connect to database
		doDB();

		//check that email is in list
		emailChecker($_POST["email"]);

		//get number of results and do action
		if (mysqli_num_rows($check_res) < 1) {
			//free result
			mysqli_free_result($check_res);

			//print failure message
			$display_block = "<p>Couldn't find your address!</p>
			<p>No action was taken.</p>";
		} else {
			//get value of ID from result
			while ($row = mysqli_fetch_array($check_res)) {
				$id = $row["id"];
			}

			//unsubscribe the address
			$del_sql = "DELETE FROM subscribers WHERE id = '".$id."'";
			$del_res = mysqli_query($mysqli, $del_sql) or die(mysqli_error($mysqli));
			$display_block = "<P>You're unsubscribed!</p>";
		}
		mysqli_close($mysqli);
	}
}
?>
<html>
<head>

</head>
<body>
<strong>Email Updates</strong>
<?php echo "$display_block"; ?>
</body>
</html>

Recommended Answers

All 2 Replies

What happens when you use this? Give more details.

I figured it out.

The problem lay with me using php5 code on a php4 server. I contacted the hosting company and they gave me i small line of code to include in the .htaccess root file to force the files to be parsed in php5 rather than php4. it worked!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.