将MYSQL与wamp和Android Studio连接时出现PHP错误

时间:2015-07-17 11:21:14

标签: php mysql mysqli

我正在尝试使用wamp服务器将我的phpadmin数据库连接到Android Studio,但不幸的是我在我的php代码中得到了以下错误,并且我尝试使用mysqli而不是mysql。你能帮忙吗?。

错误日志

错误

    <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.0010</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.0894</td><td bgcolor='#eeeeec' align='right'>310872</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>
07-17 11:39:17.449    6463-6549/com.bradvisor.bradvisor E/JSON Parser﹕ Error parsing data org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject
07-17 11:39:17.473    6463-6463/com.bradvisor.bradvisor E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.bradvisor.bradvisor, PID: 6463
    java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String org.json.JSONObject.getString(java.lang.String)' on a null object reference

DBFunctions.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 mysql 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 = mysqli_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())"); // This is Line 67 where it brings an error.
        // 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;
    }

}

?>

3 个答案:

答案 0 :(得分:1)

当您调用mysqli_*函数时,您需要传递创建数据库连接时获得的链接/引用(mysqli_connect(...);调用返回的对象)。

查询调用类似于:

mysqli_query($link, "SELECT * FROM ...");

我个人建议使用mysqli的OOP样式而不是程序。

答案 1 :(得分:1)

mysqli_query使用2个参数,但你只使用一个,你需要设置$ link参数和$ query,如下所示:

$ link = mysqli_connect(“localhost”,“my_user”,“my_password”,“world”);

mysqli_query($ link,“CREATE TEMPORARY TABLE myCity LIKE City”);

查看手册:http://php.net/manual/es/mysqli.query.php

运气!

答案 2 :(得分:0)

   mysqli_query at list Two argument pass 1 is connection string and 2 is query

for Example
$link = mysqli_connect("localhost", "my_user", "my_password", "world");
 if (mysqli_query($link, "CREATE TEMPORARY TABLE myCity LIKE City") === TRUE) {
printf("Table myCity successfully created.\n");

}