<?php
header("content-type:text/html;charset=utf-8");
$dbms = "mysql";
$dbhost = "localhost";
$dbuser = "root";
$dbpwd = "";
$dbname = "student";
$dsn = "$dbms:host=$dbhost;$ dbname";
try{
$pdo = new PDO($dsn,$dbuser,$dbpwd);
$pdo->query("set names utf8");
$sql = "insert into stu(uid,name,sex,age,grade,class,cre_time) values ('450000','susan','male','23 years old','kindergarten','first class',now( ))";
$row = $pdo->exec($sql);
echo "Affected the ".$row." row";
}catch(PDOException $e){
die("An error occurred:".$e->getMessage());
}
?>
//
stu table structure
id int(6) automatic increment
uid varchar(8)
##name varchar(12) NULL sex varchar(4) NULL##age varchar(10) NULL
grade varchar(10) NULL
##class varchar(6) NULL cre_time datetime NULL mod_time datetime NULL
. . . . .
I was careless, $dsn = "$dbms:host=$dbhost;$dbname"; This is wrong
It should be $dsn = "$ dbms:host=$dbhost;dbname = $dbname"; missing dbname=
What does the execution result show? No information? Or is there an error in the report?