mysql - Why isn't my first ever PHP class working as expected? -
i writing first ever php class, trying larn oo concepts go along. goal of code record user's ip , related parameters database might later utilize basis applying temporary ban. (no let's not discuss merits of strategy - let's maintain coding classes please!). here's have:
class banass { public function __construct(mysqli $db){ $this->db = $db; $this->visitip = $_server['remote_addr']; $this->visitagent = $_server['http_user_agent']; $this->visitdate = time(); } public function addban(){ $sql = "insert banned (visitip, visitagent, visitdate,) values (?, ?, ?)"; $stmt = mysqli_prepare($this->db, $sql); mysqli_stmt_bind_param($stmt, "ssi", $this->visitip, $this->visitagent, $this->visitdate); mysqli_stmt_execute($stmt); mysqli_stmt_close($stmt); } } $banned = new banass($db); $banned->addban(); it's worth noting have valid database connection has been opened using mysqli. have been playing whackamole error messages on this. stand at:
warning: mysqli_stmt_bind_param() expects parameter 1 mysqli_stmt, boolean given in /home/c9/public_html/admin/login.php on line 16 warning: mysqli_stmt_execute() expects parameter 1 mysqli_stmt, boolean given in /home/c9/public_html/admin/login.php on line 17 warning: mysqli_stmt_close() expects parameter 1 mysqli_stmt, boolean given in /home/c9/public_html/admin/login.php on line 18 i don't understand these messages. suggest perhaps 'mysqli_prepare' line created variable $stmt messed somehow, how?
warning: mysqli_stmt_bind_param() expects parameter 1 mysqli_stmt, boolean given in...
read closely: it's telling you're passing in boolean value when function doesn't expect there. mysqli_prepare() returns boolean false when encounters error, , that's you're attempting pass mpsqli_stmt_bind_param() function, thereby having it produce error , boolean false value, trickles down.
use mysqli_error() exact error you're getting , perchance little insight how prepare said error.
php mysql class
No comments:
Post a Comment