douqie1884 2018-01-31 02:40
浏览 71
已采纳

如何在datatables中连接postgresql?

i had created and connected datatables to postgresql.

this is my connection code. may i have two connection called pg_query, and how i can make a right code? i try for 2 days and still stuck

<?php
$conn = "host=localhost port=5432 dbname=test user=postgres password=password";
$connection = pg_connect($conn);
if (!$connection) {
print("failed");
exit;
}
else print("Connection Success");
?>

this is my datatables code

<?php
include_once("connection.php");

$params = $columns = $totalRecords = $data = array();

$params = $_REQUEST;

$columns = array( 
    0 =>'id',
    1 =>'employee_name', 
    2 => 'employee_salary',
    3 => 'employee_age'
);

$where = $sqlTot = $sqlRec = "";

$sql = "SELECT * FROM employee ORDER BY employee_name";
$sqlTot .= $sql;
$sqlRec .= $sql;


$sqlRec .=  "ORDER BY employee_name";

$queryTot = pg_query($conn, $sqlTot)or die("database error:");

$totalRecords = pg_num_rows($queryTot);

$queryRecords = pg_query($conn, $sqlRec) or die("error to fetch employees data");

while( $row = pg_fetch_row($queryRecords) ) { 
    $data[] = $row;
}   

$json_data = array(
    "draw"            => 1,   
    "recordsTotal"    => intval( $totalRecords ),  
    "recordsFiltered" => intval($totalRecords),
    "data"            => $data  
);

echo json_encode($json_data);  

print_r ($json_data);
?>

But, i'm getting error enter image description here

  • 写回答

1条回答 默认 最新

  • dougan4884 2018-01-31 02:49
    关注

    First parameter to pg_query should be connection you get from pg_connect. What you actually pass is variable $conn where you have string with connection specification (username etc).

    To fix your problem, instead of $conn use variable $connection where you store actual connection returned by pg_connect on line:

    $connection = pg_connect($conn);
    

    So for example instead of:

    $queryTot = pg_query($conn, $sqlTot)or die("database error:");
    

    there should be:

    $queryTot = pg_query($connection, $sqlTot)or die("database error:");
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥60 许可证msc licensing软件报错显示已有相同版本软件,但是下一步显示无法读取日志目录。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系