Sample code for implementing PDO-based preprocessing in php

Source: Internet
Author: User
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!

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.