mysqli_error() expects parameter 1 to be mysqli

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
danboy
Forum Newbie
Posts: 1
Joined: Sat Feb 07, 2009 1:58 pm

mysqli_error() expects parameter 1 to be mysqli

Post by danboy »

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:

Code: Select all

 
<?
//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:

Code: Select all

 
 
<?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:

Code: Select all

 
[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.
User avatar
susrisha
Forum Contributor
Posts: 439
Joined: Thu Aug 07, 2008 11:43 pm
Location: Hyderabad India

Re: mysqli_error() expects parameter 1 to be mysqli

Post by susrisha »

you have to declare the global variables outside the functions inorder to use them as global variables inside a function
Add these two lines before the doDB() function in ch19_include.php

Code: Select all

 
$mysqli = null;
$check_res =null;
 
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: mysqli_error() expects parameter 1 to be mysqli

Post by Benjamin »

Actually, the parameters are switched..

Code: Select all

 
# this is wrong, parameter one is the query, not the mysql connection resource
# $check_res = mysqli_query($mysqli, $check_sql) or die(mysqli_error($mysqli));
 
# correct:
$check_res = mysqli_query($check_sql, $mysqli) or die(mysqli_error($mysqli));
 
Also, please edit your original post and remove your username/password from the source code.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: mysqli_error() expects parameter 1 to be mysqli

Post by requinix »

astions wrote:Actually, the parameters are switched..
Actually no. It's link first, query second.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: mysqli_error() expects parameter 1 to be mysqli

Post by Benjamin »

tasairis wrote:Actually no. It's link first, query second.
Ah you are right, mysqli_query redirects to mysql_query on php.net, it was showing me the wrong function.

http://us.php.net/manual/en/mysqli.query.php
Post Reply