Jump to content

Having trouble debugging a mysqli error


k4

Recommended Posts

Hey all,

  I'm having some trouble running some mysql statements through mysqli. I've tried to debug them various ways, but to no avail. This is the offending code:

class DBOPS {
protected $config, $mysqli;

public function __construct () {
$this->config = array (...snip...);
$this->mysqli = new mysqli ($this->config['server'], $this->config['username'], $this->config['password'], $this->config['database']);
session_start();
}

public function Login ($username, $password) {
$toreturn = NULL;

//Problem area is from here...

$encryptedpassword = hash(...snip...);

print_r(array(
$this->config['usertable']['username'],
$username,
$this->config['usertable']['password'],
$encryptedpassword));

$statement = $this->mysqli->stmt_init();
$statement->prepare('SELECT * FROM users WHERE ? = ? AND ? = ?');
$statement->bind_param(
'ssss',
$this->config['usertable']['username'],
$username,
$this->config['usertable']['password'],
$encryptedpassword);

$statement->execute();

print_r($statement->affected_rows);

//To here.

if ($statement->affected_rows == 1) {
$_SESSION[$this->config['sessiondata']['username']] = $username;
$_SESSION[$this->config['sessiondata']['lastactivity']] = time();

$toreturn = TRUE;
} else {
$toreturn = FALSE;
}

$statement->close();

return $toreturn;
}

 

The print_r($statement->affected_rows); statement always returns -1, which means a query error. Is there a problem with the syntax of the predefined query in $statement->prepare('SELECT * FROM users WHERE ? = ? AND ? = ?') ? I really can't seem to find the problem in this code. There are no errors returned. The output from the print_r() statements are

Array ( [0] => Name [1] => Trey [2] => Password [3] => ...snip... )

from the first print_r and

-1

from the second.

 

And lastly, I know that DIY login systems are generally discouraged, but this is more of a research project. I would greatly appreciate any help with this.

 

Thanks.

Link to comment
https://forums.phpfreaks.com/topic/212237-having-trouble-debugging-a-mysqli-error/
Share on other sites

affected_rows is only relevant for INSERT, UPDATE, or DELETE queries.

 

num_rows would be used with a SELECT query.

 

$statement->execute() returns either TRUE on success or FALSE on failure. You would probably want to test that to determine if the query produced an error or not and then $statement->error would tell you what the error was.

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.