简体   繁体   English

如何使用PHP发送HTTP请求以使用Jaxon连接PostgreSQL数据库?

[英]How to send HTTP request with PHP to connect PostgreSQL db using Jaxon?

I'm trying to connect to a PostgreSQL database when I click on a button and display a message to notify success or fail connection. 单击按钮并显示一条消息以通知连接成功或失败时,我尝试连接到PostgreSQL数据库。

It's my first time doing such operation in web interface and using Jaxon (PHP lib to do AJAX, fork of XAJAX). 这是我第一次在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>

When I click the button from my browser I get nothing. 当我从浏览器中单击按钮时,我什么也没得到。

I want to learn from this, so, if possible, explain to me what I'm doing wrong here, or if my approach is lacking insight. 我想从中学习,因此,如有可能,请向我解释我在这里做错了什么,或者我的方法缺乏见识。

Checkout the pg_connect() documentation at https://www.php.net/manual/fr/function.pg-connect.php . https://www.php.net/manual/fr/function.pg-connect.php上检出pg_connect()文档。 You need to fix its parameters. 您需要修复其参数。

You may also want to use the developer mode of your browser to see when your application return an HTML error instead of the expected json, which in this case explains why you get nothing in your web page. 您可能还想使用浏览器的开发人员模式查看应用程序何时返回HTML错误而不是预期的json,在这种情况下,这说明了为什么您的网页没有任何内容。

Thierry 蒂埃里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM