MySQL查询问题

时间:2011-02-20 06:58:56

标签: php mysql

我收到以下错误消息。请帮我。感谢

Notice: Trying to get property of non-object in
C:\xampp\htdocs\my\include\user_functions.php on line 34

这是我的代码

$conn = db_connection();
    if($conn == false) {
        user_error('Unable to connect to database');
        return false;
    }
    $query = "UPDATE user SET passwd = '".$new_passwd."'
            WHERE username = '".$username."'  ";

    $result=$conn->query($query);
    if($result == false) {
        user_error('Query Error'.$conn->error);
        return false;
    }           

    if($result->num_rows == 1) {
        echo 'Password changed';
    } else {
            echo 'Failed ';
    }

这是我的db_connection

function db_connection() {
    $db = new mysqli('localhost','root','','php_login');
    if(!$db) {
        echo 'Could not connect to database server';
    } else {
        return $db;
    }
}

2 个答案:

答案 0 :(得分:2)

UPDATE语句不返回结果集。您想从fetch_array获得什么?

答案 1 :(得分:1)

<强>更新

class mysqli{

    public $aff_num_rows;
    //some properties
    //some properties

    //some methods
    //some methods

    public function query($sql)
    {
        $resultset = mysql_query($sql); //after query instantiate the $aff_numrows
                        //property with function 
        $this->aff_num_rows = mysql_affected_rows(); //guess you are using sqlite
        //so you might have different function
        //and you can use this property 
    }   

}

你的代码中有

if($conn->aff_num_rows == 1) {
    echo 'password changed';
} else {
        echo 'error changing pasword';
}