[英]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.