为什么我的 SELECT 语句返回数据库名称作为列名称?! (PDO 新功能)
您好,感谢您的阅读。
我是 php 新手(但我已经编程很长时间了),所以我决定使用 pdo 接口作为数据库查询的启动器。我确实放置了一个小脚本来测试,但它返回数据库名称作为列名称之一。为什么?
另外,对于 pdo 专业人士来说,一旦我实例化了一个新的 pdo 对象而不指定数据库名称,我如何选择它以防止在查询中写入“databaseName.tableName”...请参阅下面的脚本:
尝试一下 { $dbh = new PDO('mysql:host=localhost', 'root', '', array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION)); } 捕获(异常 $e) { 回显'错误:'。 $e->getMessage() 。 '
'; 回显'N°:'。 $e->getCode(); 死(); }$sth = $dbh->prepare("如果不存在则创建数据库 myTest 默认字符集 utf8 COLLATE utf8_unicode_ci"); $sth->execute();
$sth = $dbh->prepare("如果不存在则创建表 myTest.user( personID int NOT NULL AUTO_INCRMENT, 主键(人ID), 名字 varchar(15), 姓氏 varchar(15), 年龄整数(3) )"); $sth->execute();
$sth = $dbh->prepare("INSERT INTO myTest.user (FirstName, LastName, Age) VALUES(?, ?, ?)"); $sth->execute(array("Charles", "Gagnon", "28"));
$sth = $dbh->query("SELECT * FROM myTest.user"); $result = $sth->fetch(PDO::FETCH_ASSOC);
$json = json_encode($结果); print_r($json);
<代码>?>
所以是的, print_r 输出这个 json:
{"personID":"1","FirstName":"Charles","user":"28"}
很奇怪,它输出表的名称(用户)而不是“年龄”,并且 LastName 字段根本不存在......
任何帮助将不胜感激,谢谢!
Hi and thanks for reading.
I'm new to php (but I've been programming for a long time now) so I decided to use the pdo interface as a starter for my database queries. I did put a small script to test but it returns the database name as one of the columns name. Why?
Also for you pdo pros, once I instanciated a new pdo object without specifying the database name, how can I select it to prevent writing "databaseName.tableName" in my queries... See my script below:
try { $dbh = new PDO('mysql:host=localhost', 'root', '', array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION)); } catch (Exception $e) { echo 'Erreur : ' . $e->getMessage() . '
'; echo 'N° : ' . $e->getCode(); die(); }$sth = $dbh->prepare("CREATE DATABASE IF NOT EXISTS myTest DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci"); $sth->execute();
$sth = $dbh->prepare("CREATE TABLE IF NOT EXISTS myTest.user( personID int NOT NULL AUTO_INCREMENT, PRIMARY KEY(personID), FirstName varchar(15), LastName varchar(15), Age int(3) )"); $sth->execute();
$sth = $dbh->prepare("INSERT INTO myTest.user (FirstName, LastName, Age) VALUES(?, ?, ?)"); $sth->execute(array("Charles", "Gagnon", "28"));
$sth = $dbh->query("SELECT * FROM myTest.user"); $result = $sth->fetch(PDO::FETCH_ASSOC);
$json = json_encode($result); print_r($json);
?>
So yeah, the print_r outputs this json:
{"personID":"1","FirstName":"Charles","user":"28"}
Pretty weird, it outputs the name of the table (user) instead of "Age" and the LastName field isn't there at all...
Any help will be appreciated, thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
无法复制这一点。使用您的确切代码,我得到
您确定myTest
数据库和表user
不存在,其架构与您期望的不同(但不知何故)仍然适用于INSERT
语句)?编辑:如果架构不同,您的插入语句将无法工作。
只需重新设置 PDO 对象,指定
dbname
DSN 中的参数。否则,我想您可以尝试执行use;
命令。Cannot replicate this. Using your exact code, I get
Are you sure themyTest
database and tableuser
do not already exist with a different schema to what you're expecting (yet somehow still working for theINSERT
statement)?Edit: There's no way your insert statement would work if the schema was different.
Just re-instate the PDO object, specifying the
dbname
parameter in the DSN. Otherwise, I suppose you could try executing ause <database>;
command.