danboy712 Posted February 7, 2009 Share Posted February 7, 2009 Hi I have an error on a test project for a mailing list manager using php & mysql it includes two scripts one that defines functions to be included in the other: ch19_include.php: <? //set up a couple of functions for use by scripts in ch19 function doDB() { global $mysqli; //connect to server and select database; you may need it $mysqli = mysqli_connect("localhost", "root", "millcnc", "testDB"); //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)); } ?> And one that manages the mailing list table itself: <?php include("ch19_include.php"); //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=\"40\" 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> <title>Subscribe/Unsubscribe to a Mailing List</title> </head> <body> <h1>Subscribe/Unsubscribe to a Mailing List</h1> <?php echo "$display_block"; ?> </body> </html> Apache returns this: [sat Feb 07 14:50:48 2009] [error] [client 127.0.0.1] PHP Warning: mysqli_error() expects parameter 1 to be mysqli, null given in C:\\Program Files\\Apache Software Foundation\\Apache2.2\\htdocs\\ch19_include.php on line 23, referer: http://localhost/manage.php I cannot see an obvious error in either script does anyone have any advice? many thanks PS I am running the latest versions of Mysql, PHP and Apache on Windows XP. Link to comment https://forums.phpfreaks.com/topic/144215-mysqli_error-expects-parameter-1-to-be-mysqli/ Share on other sites More sharing options...
johntp Posted February 7, 2009 Share Posted February 7, 2009 hmmmm do you have mysqli enabled in php.ini? Link to comment https://forums.phpfreaks.com/topic/144215-mysqli_error-expects-parameter-1-to-be-mysqli/#findComment-756843 Share on other sites More sharing options...
danboy712 Posted February 7, 2009 Author Share Posted February 7, 2009 yes it threw up an error as soon as I started because it wasn't enabled. I fixed that it now renders a postback which is just a blank page. It should postback a message aknowledging that the user has subcribed / unsubscribed. Link to comment https://forums.phpfreaks.com/topic/144215-mysqli_error-expects-parameter-1-to-be-mysqli/#findComment-756858 Share on other sites More sharing options...
johntp Posted February 7, 2009 Share Posted February 7, 2009 so now no errors, it's just blank? try putting the following code at the top of those pages and then run it again. <?php error_reporting(E_ALL); ini_set('display_errors', 1); ?> Link to comment https://forums.phpfreaks.com/topic/144215-mysqli_error-expects-parameter-1-to-be-mysqli/#findComment-756957 Share on other sites More sharing options...
danboy712 Posted February 7, 2009 Author Share Posted February 7, 2009 Thanks very much I've altered it a bit and it now works added in that snippet for the sake of being of thorough aswell. Link to comment https://forums.phpfreaks.com/topic/144215-mysqli_error-expects-parameter-1-to-be-mysqli/#findComment-756988 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.