php - Execute query inside a while loop.. MySQli -
i'm trying update current database plain text passwords hashes passwords using crypt()
.. i'm trying without users having change passwords (this instable approach) code so:
$query = $database->prepare("select id,username,password userlist"); $query->execute(); $query->bind_result($id,$username,$password); while ($query->fetch()){ $hashed = $framework->hash_password($password); $secondary_query = $database->prepare("update userlist set password=?, salt=? id=?"); $secondary_query->bind_param('ssi', $hashed['password'],$hashed['salt'],$id); $secondary_query->execute(); $secondary_query->close(); } $query->close();
i'm getting error:
fatal error: call member function bind_param() on non-object in c:\inetpub\www\adminchangetextpass.php on line 24
now. know column names 100% match aswell database names. know variables correctly set.
debugging
debugging:
$query = $database->prepare("select id,username,password userlist"); $query->execute(); $query->bind_result($id,$username,$password); while ($query->fetch()){ echo $password."<br>"; } $query->close(); // returns: //test //test then: $query = $database->prepare("select id,username,password userlist"); $query->execute(); $query->bind_result($id,$username,$password); while ($query->fetch()){ print_r($framework->hash_password($password)); } $query->close(); /* returns: array ( [salt] => ÛûÂÒs8q-h¸Ý>c"ÿò [password] => Ûûj1qnm/ui/16 ) array ( [salt] => ÛûÂÒs8q-h¸Ý>c"ÿò [password] => Ûûj1qnm/ui/16 ) */
database schema
create table if not exists `userlist` ( `id` int(255) not null auto_increment, `username` varchar(255) not null, `password` varchar(255) not null, `salt` text not null, primary key (`id`) ) engine=innodb default charset=latin1 auto_increment=9 ; -- -- dumping data table `userlist` -- insert `userlist` (`id`, `username`, `password`, `salt`) values (1, 'test', 'test', ''), insert `userlist` (`id`, `username`, `password`, `salt`) values (2, 'test', 'test', '');
having code this:
$secondary_query = $database->prepare("update userlist set password=? id=?"); $query = $database->prepare("select id,username,password userlist limit 1"); var_dump($secondary_query); #$query->execute(); #$query->bind_result($id,$username,$password); # while ($query->fetch()){ # $hashed = $framework->hash_password($password); # $secondary_query = $database->prepare("update userlist set password=? id=?"); # $secondary_query->bind_param('ssi', $hashed['password'],$hashed['salt'],$id); # $secondary_query->execute(); # $secondary_query->close(); # } #$query->close();
the var_dump($secondary_query);
returns:
object(mysqli_stmt)#3 (10) { ["affected_rows"]=> int(-1) ["insert_id"]=> int(0) ["num_rows"]=> int(0) ["param_count"]=> int(2)
["field_count"]=> int(0) ["errno"]=> int(0) ["error"]=> string(0) "" ["error_list"]=> array(0) { } ["sqlstate"]=> string(5) "00000" ["id"]=> int(1) }
and var_dump($query);
returns:
object(mysqli_stmt)#4 (10) { ["affected_rows"]=> int(-1) ["insert_id"]=> int(0) ["num_rows"]=> int(0) ["param_count"]=> int(0) ["field_count"]=> int(3) ["errno"]=> int(0) ["error"]=> string(0) "" ["error_list"]=> array(0) { } ["sqlstate"]=> string(5) "00000" ["id"]=> int(2) }
as cannot submit answer of yet.. working code followed:
$query = $database->prepare("select id,username,password userlist"); $query->execute(); $query->bind_result($id,$username,$password); $query->store_result(); while ($query->fetch()){ $hashed = $framework->hash_password($password); $secondary_query = $database->prepare("update userlist set password=?, salt=? id=?"); $secondary_query->bind_param('ssi', $hashed['password'],$hashed['salt'],$id); $secondary_query->execute(); $secondary_query->close(); } $query->close();
edit: enabling more verbose error reporting key in helping debug problem: mysqli_report(mysqli_report_all)
.
the following answer question posted here.
you can't have 2 simultaneous queries because mysqli uses unbuffered queries default (for prepared statements; it's opposite vanilla mysql_query
). can either fetch first 1 array , loop through that, or tell mysqli buffer queries (using $query->store_result()
).
see here details.
Comments
Post a Comment