note 120476 added to mysqli-stmt.bind-param

From: Date: Tue, 17 Jan 2017 16:22:52 +0000
Subject: note 120476 added to mysqli-stmt.bind-param
Groups: php.notes 
Request: Send a blank email to php-notes+get-208742@lists.php.net to get a copy of this message
Most people make this harder than it has to be and don't fully explain some basics to get your started. So here is an example to go by. In PDO nothing can be NULL (or a variable that is bound be empty), so I have an example below to help with that and other ways to bind parameters in an easy way to follow. <? // define database related variables $database = 'database_here'; $host = 'localhost'; $user = db_username'; $pass = 'db_password'; // try to conncet to database $dbh = new PDO("mysql:dbname={$database};host={$host};port={3306}", $user, $pass); if(!$dbh){ echo "unable to connect to database"; } // Create placeholder variables $username = "email@email.com"; $password = "Password"; //can also use isset instead of !empty if (!empty($username) && !empty($password)) { /* //option 1 build one param at a time //build a list because you can not have NULL for a param for binding, you can even build these one at a time $params[":username"] = $username; $params[":password"] = $password; $boundusername = "username = :username "; $boundpassword = "password = :password "; */ // Option2 // another way to do this but as a group, and would work best with this scenario because if one variable ($username or $password) is empty then it produces an error below $result == 0 $params = array( ':username' => $username , ':password' => $password ); /* //Option 3 // Don't make an array with a variable but put it in directly $query->execute(array(':username' => $username, ':password' => $password)); */ } // Create query /* // option 1 $sql = "SELECT * FROM table WHERE $boundusername AND $boundpassword"; */ //option 2 // if :username or :password is NULL (or empty) will cause error $sql = "SELECT * FROM table WHERE username = :username AND password = :password"; /* //Option 3 //Manual array input $query->execute(array(':username' => $username, ':password' => $password)); */ // Prepare query using PDO $query = $dbh->prepare($sql); // Execute query $result = $query->execute($params); // It may be tempting to do this, but hits your database again , so use count($result) instead // if($query->rowCount() == 0){ if(count($result) == 0){ //if $result == 0 then was an error // Send to login page with an error message wrong username or password header('Refresh: 0; url = "login.php?error=2"'); }else{ //if $result == 1 then was a success // Add user to session and take him to home page to dashboard etc. header('Refresh: 0; url = "home.php"'); } }else{ // Go back to login page with error message. header('Refresh: 0; url = "login.php?error=1"'); } ?> ---- Server IP: 72.52.91.14 Probable Submitter: 208.98.133.251 ---- Manual Page -- http://php.net/manual/en/mysqli-stmt.bind-param.php Edit -- https://master.php.net/note/edit/120476 Del: integrated -- https://master.php.net/note/delete/120476/integrated Del: useless -- https://master.php.net/note/delete/120476/useless Del: bad code -- https://master.php.net/note/delete/120476/bad+code Del: spam -- https://master.php.net/note/delete/120476/spam Del: non-english -- https://master.php.net/note/delete/120476/non-english Del: in docs -- https://master.php.net/note/delete/120476/in+docs Del: other reasons-- https://master.php.net/note/delete/120476 Reject -- https://master.php.net/note/reject/120476 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#208742) next »