PHP:在非对象上调用成员函数query()

时间:2014-07-22 17:12:00

标签: php mysql mysqli

我一直在努力解决这个问题几个小时,我不知道出了什么问题。我知道它正好盯着我。希望你们能发现我所缺少的东西。从其他人的代码我看过我的应该'应该'工作...

我在一个单独的文件中连接到我的数据库,我们称之为db.php:

 $mysqli = new mysqli($db_host,$db_user,$db_password,$db_database);

    if ($mysqli->connect_error) {
        die('Connect Error (' . $mysqli->connect_errno . ') '
                . $mysqli->connect_error);
    }

然后该文件包含在我的user_experience_.php文件中,该文件包含:

    require_once('db.php');

    function create_user($first_name, $last_name, $username, $password,$email, $phone, 
                                    $company,$address1, $address2, $city, $state, $zip,
                                    $country){

                //Does user info exist already
                if($result = $mysqli->query("SELECT customer_id FROM AUSRE_customers WHERE email='$email';"))
                    if($result['customer_id'] !

= "")
                    return -3;

            //Hash the password
            $password_hashed = create_hash($password);


            //Inserting to AUSRE_customers
            if($result = $mysqli->query("INSERT INTO AUSRE_customers SET firstname='$firstname',
                    lastname='$last_name', usesrname='$username', password='$password_hashed' email='$email',
                    phone='$phone', company='$company', address_line_1='$address1',
                    address_line_2='$address2', city='$city', state='$state',
                    zip='$zip', country='$country';")){/*Success*/}
            else{
                return -1;
            }

            //Get Customer ID for class object
            if($result = $mysqli->query("SELECT customer_id FROM AUSRE_customers WHERE email='$email';")->fetch_assoc()){
                $this->$customer_id = $result['customer_id'];
            }
            else{
                return -2;
            }

            //Set account creation date
            if($result = $mysqli->query("INSERT INTO AUSRE_customer_info SET 
                    customer_id='$this->$customer_id', date_of_account_creation=NOW();")){/*Success*/}
                else{
                    return -2;
                    }
        }


    }// End Customer Class

我已确认对象$mysqli已在user_experience_.php中初始化。第一次调用$mysqli->query();给出了以下错误:

  

致命错误:在..

中的非对象上调用成员函数query()

任何想法

1 个答案:

答案 0 :(得分:2)

$mysqlicreate_user函数的范围内不可见,因为它在外部声明。您需要将其作为参数传递给create_user()或在其正文中使用(禁止 - 否)global $mysql;