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
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.

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.