[英]pg_connect does not return false on failure
I am trying to connect to postgres database using pg_connect in php. 我正在尝试使用php中的pg_connect连接到postgres数据库。 If I provide wrong credentials for testing it does not return false.
如果我提供错误的凭据进行测试,则不会返回false。
Instead its return below error 而是返回错误以下
Description: 描述:
pg_connect(): Unable to connect to PostgreSQL server: could not connect to server: Connection refused pg_connect():无法连接到PostgreSQL服务器:无法连接到服务器:连接被拒绝
Try this may be helpful 试试这个可能会有所帮助
<?php
function getdb() {
$db = pg_connect("...") or die('connection failed');
return $db;
}
?>
I think the problem here is that you are trying to print the result, but printing a value of false prints nothing, so you are just seeing the warning that pg_connect generates. 我认为这里的问题是您试图打印结果,但是打印false值不会打印任何内容,因此您只是看到pg_connect生成的警告。
print(false); //prints nothing
If you want to see the actual value of a variable, use var_dump() instead: 如果要查看变量的实际值,请使用var_dump()代替:
$result = pg_connect(...);
var_dump($result);
As some one said in comment, you should check here: how to catch pg_connect() function error? 就像有人在评论中说的那样,您应该在这里检查: 如何捕获pg_connect()函数错误?
You could hide the error by using @ and do something like this: 您可以使用@隐藏错误并执行以下操作:
<?php
$db = @pg_connect("...") or die("failed");
if( $db == "failed"){
// false
}
else {
// true
}
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.