Follow the php[14]-pdo of Baidu learn the pre-statement 2

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.