php - 使用PHP检查数据库是否存在于postgreSQL中

标签 php sql database postgresql

我想知道是否有人能告诉我是否可以使用 PHP 检查 postgresql 数据库是否存在?

我正在编写一个 PHP 脚本,我只希望它在数据库不存在的情况下创建它,但到目前为止还没有看到如何实现它。

谢谢


我试过了

$cmd = 'psql -U postgres -c "SELECT schema_name FROM information_schema.schemata WHERE schema_name = 'portal';"';
    exec($cmd);

我得到了

PHP Parse error:  syntax error, unexpected 'portal'

最佳答案

单引号用于初始化语句时出现语法错误。

$cmd = 'psql -U postgres -c "SELECT schema_name FROM information_schema.schemata WHERE schema_name = \'portal\';"';
exec($cmd);

您应该使用 PDO 而不是 exec,但这可能对您有用。

关于php - 使用PHP检查数据库是否存在于postgreSQL中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37948913/

相关文章:

MySQL 守护进程拒绝以 "Can' 启动服务器 : Bind on TCP/IP port: Address already in use"(it's not).

php - 从 PHP 执行 Python - 选择 Python 版本 (Linux - CentOS)

php - firefox 将 xlsx 文件检测为 97-2003 工作簿 (.xls),将文件另存为 example.xlsx.xls

php - PHP MYSQL、latin1_swedish_ci 和 utf8_general_ci 的非法混合排序规则

mysql - 无法添加外键约束错误 1215

mysql - 在 SELECT 查询中表达公式

php - 查询 PHP 信号量而不阻塞?

sql - Postgres 中的时间序列

android - 使用 RecyclerView 的房间数据库 View

sql - 如何找到一个数字的范围,其中范围动态来自另一个表?