问题描述
我正在尝试创建CMS,并且希望简化客户端的工作,因此我自己创建数据库.尝试执行此操作时遇到了问题.未创建数据库,因此无法使用PDO连接进行连接,并且已弃用mysql
,因此无法使用它.对于在创建数据库之前如何创建PDO连接的问题,您有什么建议吗?像mysql_select_db()
替代品一样?
I'm trying to create a CMS and I want to make my client life easier so I create the database by myself.I encountered a problem while trying to do this. The database isn't created so I can't connect with a PDO connection and mysql
is deprecated so I can't use it. Do you have any advice for me on how I can create a PDO connection before the database is created? like a mysql_select_db()
alternative?
推荐答案
您可以以此启动PDO连接,如果我输入错了,请纠正我:
You can start a PDO connection by this, correct me if I'm wrong:
$db = new PDO("mysql:host=localhost", 'user', 'pass');
与普通连接不同的是,您看到的是被移除的零件dbname=[xx]
.
The difference from this with a normal connection is the removed part dbname=[xx]
, as you see.
当您要使用UTF8连接时,还有一个免费提示:
Also a free tip when you want to use a UTF8 connection:
$db = new PDO("mysql:host=localhost;charset=UTF-8", 'user', 'pass');
为了注释Yehonatan
,您可以通过以下方式选择数据库:
In order to the comment of Yehonatan
you can select a database by:
$db->exec('USE databaseName');
这篇关于没有数据库名称的Pdo连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!