PHP错误 - 未定义的方法DB_Connect :: query()

时间:2015-07-17 19:27:07

标签: php mysql mysqli

我在尝试将mysqli方法和函数实现到我的php文件时遇到错误,我在下面的行中收到错误,请指教。

$result = $this->db->query("INSERT INTO users(unique_id, firstname, lastname, email, username, encrypted_password, salt, created_at) VALUES('$uuid', '$fname', '$lname', '$email', '$uname', '$encrypted_password', '$salt', NOW())");      

我在第67行得到了一个错误,我在下面说明了这一点。

第67行 -

$result = $this->db->query("INSERT INTO users(unique_id, firstname, lastname, email, username, encrypted_password, salt, created_at) VALUES('$uuid', '$fname', '$lname', '$email', '$uname', '$encrypted_password', '$salt', NOW())");

错误CatLog

07-17 19:21:10.452    5504-5663/com.bradvisor.bradvisor E/JSON﹕ <br />
    <font size='1'><table class='xdebug-error xe-fatal-error' dir='ltr' border='1' cellspacing='0' cellpadding='1'>
    <tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> Fatal error: Call to undefined method DB_Connect::query() in C:\wamp\www\bradvisor_login_api\include\DB_Functions.php on line <i>67</i></th></tr>
    <tr><th align='left' bgcolor='#e9b96e' colspan='5'>Call Stack</th></tr>
    <tr><th align='center' bgcolor='#eeeeec'>#</th><th align='left' bgcolor='#eeeeec'>Time</th><th align='left' bgcolor='#eeeeec'>Memory</th><th align='left' bgcolor='#eeeeec'>Function</th><th align='left' bgcolor='#eeeeec'>Location</th></tr>
    <tr><td bgcolor='#eeeeec' align='center'>1</td><td bgcolor='#eeeeec' align='center'>0.0200</td><td bgcolor='#eeeeec' align='right'>269136</td><td bgcolor='#eeeeec'>{main}(  )</td><td title='C:\wamp\www\bradvisor_login_api\index.php' bgcolor='#eeeeec'>..\index.php<b>:</b>0</td></tr>
    <tr><td bgcolor='#eeeeec' align='center'>2</td><td bgcolor='#eeeeec' align='center'>0.1260</td><td bgcolor='#eeeeec' align='right'>309464</td><td bgcolor='#eeeeec'>DB_Functions->storeUser(  )</td><td title='C:\wamp\www\bradvisor_login_api\index.php' bgcolor='#eeeeec'>..\index.php<b>:</b>136</td></tr>
    </table></font>

DBFunction.PHP文件

<?php

class DB_Functions {

    private $db;

    //put your code here
    // constructor
    function __construct() {
        require_once 'DB_Connect.php';
        // connecting to database
        $this->db = new DB_Connect();
        $this->db->connect();
    }

    // destructor
    function __destruct() {

    }


    /**
     * Random string which is sent by mail to reset password
     */

public function random_string()
{
    $character_set_array = array();
    $character_set_array[] = array('count' => 7, 'characters' => 'abcdefghijklmnopqrstuvwxyz');
    $character_set_array[] = array('count' => 1, 'characters' => '0123456789');
    $temp_array = array();
    foreach ($character_set_array as $character_set) {
        for ($i = 0; $i < $character_set['count']; $i++) {
            $temp_array[] = $character_set['characters'][rand(0, strlen($character_set['characters']) - 1)];
        }
    }
    shuffle($temp_array);
    return implode('', $temp_array);
}


public function forgotPassword($forgotpassword, $newpassword, $salt){
    $result = mysqli_query("UPDATE `users` SET `encrypted_password` = '$newpassword',`salt` = '$salt' 
                          WHERE `email` = '$forgotpassword'");

if ($result) {

return true;

}
else
{
return false;
}

}
/**
     * Adding new user to mysqli database
     * returns user details
     */

    public function storeUser($fname, $lname, $email, $uname, $password) {
        $uuid = uniqid('', true);
        $hash = $this->hashSSHA($password);
        $encrypted_password = $hash["encrypted"]; // encrypted password
        $salt = $hash["salt"]; // salt
        $result = $this->db->query("INSERT INTO users(unique_id, firstname, lastname, email, username, encrypted_password, salt, created_at) VALUES('$uuid', '$fname', '$lname', '$email', '$uname', '$encrypted_password', '$salt', NOW())");      
  // check for successful store
        if ($result) {
            // get user details 
            $uid = mysqli_insert_id(); // last inserted id
            $result = mysqli_query("SELECT * FROM users WHERE uid = $uid");
            // return user details
            return mysqli_fetch_array($result);
        } else {
            return false;
        }
    }

    /**
     * Verifies user by email and password
     */
    public function getUserByEmailAndPassword($email, $password) {
        $result = mysqli_query("SELECT * FROM users WHERE email = '$email'") or die(mysqli_error());
        // check for result 
        $no_of_rows = mysqli_num_rows($result);
        if ($no_of_rows > 0) {
            $result = mysqli_fetch_array($result);
            $salt = $result['salt'];
            $encrypted_password = $result['encrypted_password'];
            $hash = $this->checkhashSSHA($salt, $password);
            // check for password equality
            if ($encrypted_password == $hash) {
                // user authentication details are correct
                return $result;
            }
        } else {
            // user not found
            return false;
        }
    }


 /**
     * Check user is existed or not
     */
    public function isUserExisted($email) {
        $result = mysqli_query("SELECT email from users WHERE email = '$email'");
        $no_of_rows = mysqli_num_rows($result);
        if ($no_of_rows > 0) {
            // user existed 
            return true;
        } else {
            // user not existed
            return false;
        }
    }

    /**
     * Encrypting password
     * returns salt and encrypted password
     */
    public function hashSSHA($password) {

        $salt = sha1(rand());
        $salt = substr($salt, 0, 10);
        $encrypted = base64_encode(sha1($password . $salt, true) . $salt);
        $hash = array("salt" => $salt, "encrypted" => $encrypted);
        return $hash;
    }

    /**
     * Decrypting password
     * returns hash string
     */
    public function checkhashSSHA($salt, $password) {

        $hash = base64_encode(sha1($password . $salt, true) . $salt);

        return $hash;
    }

}

?>

1 个答案:

答案 0 :(得分:0)

您好像没有使用您期望的课程版本。你可以通过

进行测试
class ... {
  function LoginUsers($username,$password){
    ..
    foo($this->db, 'query');
    $query = $this->db->query("SELECT * FROM users ...")
    ..
  }
  ...
}

function foo($obj, $testFnExists=null) {
    $abort = false;
    $ro = new ReflectionObject($obj);
    printf("<pre>\nClass: %s\n", $ro->getName()); 
    printf("defined at %s@%d\n", $ro->getFileName(), $ro->getStartLine());
    printf("object has the following public methods:\n");
    foreach( $ro->getMethods(Reflectionmethod::IS_PUBLIC) as $m ) {
        printf("  %s\n", $m->getName());
    }
    if ( !is_null($testFnExists) ) {
        if ( !$ro->hasMethod($testFnExists) ) {
            $abort = true;
            $methodExists = 'no';
        }
        else {
            $methodExists = 'yes';
        }
        printf("method '%s' exists: %s\n", $testFnExists, $methodExists);
    }
    printf("</pre>\n");
    flush();
    if ( $abort ) {
        die;
    }
}

//输出应该是     

    Class: dbConnection
    defined at test.php@4
    object has the following public methods:
      foo1
      foo2
    method 'query' exists: no