In $sql = $pdo-Prepare ("insert into Users (Gold,user,password) values (?,?,?)") , we can use not only the question mark but also the substitute
: Name
That is
$sql = $pdo Prepare ("Insert into users (Gold,user,password) VALUES (: Gold,:user,:p assword)");
So write the binding variable (Bindparam) when you write it:
$sql = $pdo ->prepare ("Insert into users (Gold,user,password) VALUES (: Gold,:users,:p assword) ");
$sql ->bindparam (: Gold, $gold ); // bind to $gold variable. The colon of the first argument can be removed. $sql ->bindparam (: User, $users ); $sql ->bindparam (:p assword, $password ) $gold = 12; $users = "aaaaaaa" $password = "BBBBBBBBB";
Using a question mark can be equivalent to an indexed array, the biggest trouble is that you have to have numbers, each time you need to have numbers, it is cumbersome to reinsert.
But we can do that by outputting these arrays directly in execute.
For example, a question mark (that is, an indexed array):
Execute (array(1, "admin", "123465"), $sql );
In addition to the colon (that is, the associative array):
$sql , execute (array(": Gold" = "1", ": User" and "admin", ":p assword" and "123456"));
DEMO:
<?PHPTry{ $pdo=NewPDO ("Mysql:host=localhost;dbname=test", "Root", "" ");}Catch(pdoexception$e){ Echo $e-getmessage (); Exit;} Echo"The PDO object was created successfully. <br/>";Try { $sql=$pdo->prepare ("INSERT into users (Gold,user,password) VALUES (: Gold,:user,:p assword)"); $sqlExecute (Array(": Gold" = "1", "User" and "admin", ":p assword" and "123456"));} Catch(Exception $e) { Echo $e-getmessage (); Exit;} Echo"The SQL statement executed successfully. "; ?>
View Code
Output effects such as:
But in fact these are not the simplest.
We can do this:
$sql , execute (array($_get[' id ']));
CODE:
<?PHPHeader("content-type:text/html; Charset=utf-8 ");Try{ $pdo=NewPDO ("Mysql:host=localhost;dbname=test", "Root", "" ");}Catch(pdoexception$e){ Echo $e-getmessage (); Exit;} Echo"The PDO object was created successfully. <br/>";Try { $sql=$pdo->prepare ("INSERT into users (Gold,user,password) VALUES (: Gold,:user,:p assword)"); $sqlExecute ($_get);} Catch(pdoexception$e) { Echo $e-getmessage (); Exit;} Echo"The SQL statement executed successfully. "; ?>
View Code
url:http://127.0.0.1/x.php?gold=2&user=def&password=6666
Is this more straightforward than the previous MySQL implementation?
Follow the php[14]-pdo of Baidu learn the pre-statement 2