A simple example of PDO’s function of connecting database and inserting data in PHP


The example of this paper describes the function of PDO connecting database and inserting data in PHP. To share with you for your reference, as follows:

create profile


$db_type = "MySQL"; // database type
$host = "localhost"; // host name
$dbname = "test"; // database name
$username = "root"; // user name
$password = "root"; // password
$dsn = "{$db_Type}:host={$host};dbname={$dbName}";

PDO insert database


header('Content-type:text/html; charset=utf-8');
require 'pdo_config.php';
    $PDO = new PDO ($DSN, $username, $password); // create a connection object
    $PDO - > exec ('set names utf8 '); // set the code
    $SQL = "insert student (name, email) values ('li Si ',' 123 @ QQ. Com ')";
}catch (PDOException $e){
    Die ('operation failed'. $e - > getmessage());
//Close connection
$pdo = null;

For more information about PHP, readers who are interested in PHP, please refer to the following topics: summary of PHP database operation skills based on PDO, summary of PHP + mysqli database programming skills, introduction to PHP object-oriented programming, summary of PHP string usage, introduction to PHP + MySQL database operation and summary of PHP common database operation skills

I hope this article is helpful for PHP programming.