[英]How to send HTTP request with PHP to connect PostgreSQL db using Jaxon?
单击按钮并显示一条消息以通知连接成功或失败时,我尝试连接到PostgreSQL数据库。
这是我第一次在Web界面中使用Jaxon(PHP lib执行AJAX,XAJAX的分支)进行此类操作。
<?php
include "vendor/autoload.php";
use Jaxon\Response\Response;
use Jaxon\jaxon;
class Db_connection
{
public function test() {
$host = "host = localhost";
$port = "port = 5432";
$dbname = "dbname = test";
$user = "user = test";
$password = "password = test";
$response = new Response();
$textFail = "Error : Unable to open database";
$textValid = "Opened database successfully";
$db = pg_connect("$host $port $dbname $user $password" );
if(!$db){
$response->alert($textFail);
return $response;
}else {
$response->alert($textValid);
return $response;
}
}
}
$jaxon = jaxon();
$jaxon->register(Jaxon::CALLABLE_OBJECT, new Db_connection());
$jaxon->processRequest();
?>
<!doctype html>
<html>
<head>
<title>Jaxon Simple Test</title>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="">
<meta name="author" content="">
<link rel="icon" href="/favicon.ico">
</head>
<body>
<input type="button" value="Submit" onclick="JaxonDb_connection.test()" />
</body>
<?php
echo $jaxon->getJs();
echo $jaxon->getScript();
?>
</html>
当我从浏览器中单击按钮时,我什么也没得到。
我想从中学习,因此,如有可能,请向我解释我在这里做错了什么,或者我的方法缺乏见识。
在https://www.php.net/manual/fr/function.pg-connect.php上检出pg_connect()文档。 您需要修复其参数。
您可能还想使用浏览器的开发人员模式查看应用程序何时返回HTML错误而不是预期的json,在这种情况下,这说明了为什么您的网页没有任何内容。
蒂埃里
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.