This article mainly introduces the implementation of PDO-based preprocessing in php, and analyzes the related operation skills and precautions of pdo preprocessing in php based on examples, for more information about how to implement PDO-based preprocessing in php, see the next article. This article analyzes the related operation skills and precautions for implementing pdo preprocessing in php based on examples, for more information, see
This example describes how to implement PDO-based preprocessing in php. We will share this with you for your reference. The details are as follows:
$ Servername = "localhost"; $ username = "root"; $ password = "admin"; // $ dbname is my test database $ dbname = "test "; try {$ conn = new PDO ("mysql: host = $ servername; dbname = $ dbname", $ username, $ password ); // Set pdo error exception handling $ conn-> setAttribute (PDO: ATTR_ERRMODE, PDO: ERRMODE_EXCEPTION ); // pre-process the SQL statement and bind the parameter $ stmt = $ conn-> prepare ("INSERT INTO user (user_first, user_last, age) VALUES (: user_first,: user_last,: age) "); $ stmt-> bindParam (": user_first ", $ u Ser_first); $ stmt-> bindParam (": user_last", $ user_last); $ stmt-> bindParam (": age", $ age ); // Insert record to be executed $ user_first = "xiao"; $ user_last = "liang"; $ age = 15; $ stmt-> execute (); // Insert record to be executed $ user_first = "xiao"; $ user_last = "cheng"; $ age = 23; $ stmt-> execute (); echo "News record created successfully! ";} Catch (PDOException $ e) {echo" Error: ". $ e-> getMessage ();} $ conn = null;
The above is a detailed description of the sample code for implementing PDO-based preprocessing in php. For more information, see other related articles in the first PHP community!