我是php的初学者,有一些问题:
我有一个 php文件有一个函数 create_dump(),我在其中使用 pg_dump 转储我的 postgresql数据库 strong>命令。
现在从linux终端执行php文件时,它要求终端上的 postgresql密码。
如果用户无意中为数据库提供了错误的密码,我想在我的php文件中添加错误处理点。
换句话说:
如何从我的php文件中捕获 pg_dump (postgresql命令)执行错误?
谢谢!
答案 0 :(得分:0)
根据您提供的信息,我有一个解决方法,您想要做什么。首先,不要使用'>'来管理转储的输出你将需要使用--file = some_file.sql标志来代替同样的工作。
现在,即使你将stderr发送到stdout,似乎passthru调用的$输出也不会捕获输出,所以这就是可行的。您可以使用--file = flag修改命令并将命令的输出(包含错误)传递给日志文件,同时确保还将stderr发送到stdout(在命令后执行2>& 1)为了捕获错误。
以下是代码:
// notice the file= flag and the piping of error to the file
$command=
"/usr/bin/pg_dump --a --no-owner --no-acl --attribute-inserts ".
"--disable-dollar-quoting --no-tablespaces ".
"--file={$path['schema_path']}schema-{$latest_update}.sql ".
"--host={$db['hostname']} --user={$db['username']} ".
"--password --port={$db['port']} {$db['database']} > pg_dump_error.log 2>&1";
// no output to capture so no need to store it
passthru($command);
// read the file, if empty all's well
$error = file_get_contents('pg_dump_error.log');
if(trim($error) != '')
exit("PG_DUMP ERRROR:\n-----\n$error\n");
希望这或多或少是你想要的。