<?php
try {
$pdo = new PDO('mysql:host = localhost;dbname = test', 'root', '');
//exec():执行一条sql语句返回受影响的记录的条数,去过没有受影响的记录,他返回0
//exec对select不起作用
$sql = <<<EOF
create table if not exists user(
id int unsigned auto_increment key,
username varchar(20) not null unique,
password char(32) not null,
email varchar(30) not null
);
EOF;
$res = $pdo ->exec($sql);
var_dump($res);
} catch (PDOException $e) {
$e ->getMessage();
}
哥们,解决了吗,我的没有空格,也出这个问题,显示连接成功,返回的对象是空的
$pdo = new PDO('mysql:host = localhost;dbname = test', 'root', '');
把这句里面的mysql:host = localhost;dbname = test的空格全部去掉就OK了