Bind MySQL Variables

Source: Internet
Author: User

I will go to 51job for an interview today .. The examiner asked about the MySQL variable binding concept.

This is because the project has never been used before, and the MySQL research is insufficient. You just need to ask--back to Baidu and try again.

It is really a good thing.

The availability of variable binding is unquestionable. The 5% performance improvement on large systems is good. In addition, the security of variable binding can well regulate the verification of SQL statements, avoid writing verification statements independently,

Bind variables is recommended.

<? PHP  $ Mysqli = New Mysqli ("localhost", "root", "123321", "test" );  $ Sql1 = "Set @ profiling = 1" ;  $ Result1 =$ Mysqli -> Query ( $ Sql1  );  //  Prepare a statement on the server and insert the statement.                  $ SQL = "Insert into T (name, sex) values (?,?) " ;  $ Stmt = $ Mysqli -> Prepare ( $ SQL  );  //  Each placeholder? Pass number (BIND parameter) I D S B                  $ Stmt -> Bind_param ("Si ", $ Name , $ Sex  );  $ Name = "Andy" ;  $ Sex = 0 ;  //  Run                  $ Stmt -> Execute ();  $ Name = "Mandy" ;  $ Sex = 1 ;  //  Run                  $ Stmt -> Execute ();  $ Name = "Michael" ;  $ Sex = 0 ;  //  Run                  $ Stmt -> Execute ();  $ Name = "Happy";  $ Sex = 1 ;  //  Run                  $ Stmt -> Execute ();  $ Name = "Php" ;  $ Sex = 1 ;  //  Run                  $ Stmt ->Execute ();  $ Name = "MySQL" ;  $ Sex = 1 ;  //  Run                  $ Stmt -> Execute ();  $ Name = "Linux" ;  $ Sex = 1 ;  // Run                  $ Stmt -> Execute ();  $ Name = "Oracle" ;  $ Sex = 1 ;  //  Run                  $ Stmt -> Execute ();  $ Name = "Unix" ;  $ Sex = 1;  //  Run                  $ Stmt -> Execute ();  $ Name = "Cisco" ;  $ Sex = 1 ;  //  Run                  $ Stmt -> Execute ();  $ Stmt ->Close ();  $ Sql2 = "Show profiles" ;  $ Result2 = $ Mysqli -> Query ( $ Sql2  );  Echo '<Table border = 1 align = "center" width = 800>' ;  While ( $ Rows = $ Result2 ->Fetch_assoc ()){  Echo '<Tr align = "center">' ;  Foreach ( $ Rows   As   $ Value  ){  Echo '<TD> '. $ Value . '</TD>' ;}  Echo '</Tr>' ;  $ I = 0 ;  $ I = $ I + $ Rows ["Duration" ];}  Echo '</Table>' ;  Echo   $ I  ; ?>
<?PHP  $ Mysqli = New Mysqli ("localhost", "root", "123321", "test" );  $ Sql1 = "Set @ profiling = 1" ;  $ Result1 = $ Mysqli -> Query ( $ Sql1  );  $ Nobind = Array ("Insert into T (name, sex) values ('andy ', 0)", "insert into T (name, sex) values ('mandy', 1 )", "insert into T (name, sex) values ('Michael ', 0)", "insert into T (name, sex) values ('Happy', 0 )", "insert into T (name, sex) values ('php', 0)", "insert into T (name, sex) values ('mysql, 0 )", "insert into T (name, sex) values ('linux ', 0)", "insert into T (name, sex) values ('oracle', 0 )", "insert into T (name, sex) values ('cisco ', 0)", "insert into T (name, sex) values ('unix', 0 )" );  Foreach ( $ Nobind  As   $ SQL  ){  Echo   $ SQL  ;  $ Result = $ Mysqli -> Query ( $ SQL  );}  $ Sql2 = "Show profiles" ;  $ Result2 = $ Mysqli -> Query ( $ Sql2  );  Echo '<Table border = 1 align = "center" width = 800>' ;  While ( $ Rows = $ Result2 -> Fetch_assoc ()){  Echo '<Tr align = "center">' ;  Foreach ( $ Rows  As   $ Value  ){  Echo '<TD> '. $ Value . '</TD>' ;}  Echo '</Tr>' ;  $ I = 0 ;  $ I = $ I +$ Rows ["Duration" ];}  Echo '</Table>' ;  Echo   $ I  ;  $ Mysqli -> Close (); ?>

This article is from "fenghao.cn's soft .."

Related Article

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.