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

如何在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

图片转代码服务由CSDN问答提供 功能建议

我创建并将数据表连接到postgresql。

这是我的连接代码。 我可以有两个名为pg_query的连接,以及我如何制作正确的代码? 我试了2天仍然坚持

 &lt;?php 
 $ conn =“host = localhost port = 5432 dbname = test user = postgres password = password”; \  n $ connection = pg_connect($ conn); 
if(!$ connection){
print(“failed”); 
exit; 
} 
else print(“Connection Success”); 
?&gt; 
 <  / code>  
 
 

这是我的数据表代码

 &lt;?php 
include_once(“connection.php”); 
 \  n $ params = $ columns = $ totalRecords = $ data = array(); 
 
 $ params = $ _REQUEST; 
 
 $ columns = array(
 0 =&gt;'id',
 1 =  &gt;'employee_name',
 2 =&gt;'employee_salary',
 3 =&gt;'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)或die(“数据库错误:”); 
 
 $ totalRecords = pg_num_rows($ queryTot); 
 
 $ queryRecords = pg_query($ conn,$ sqlRec)或 die(“获取员工数据的错误”); 
 
而($ row = pg_fetch_row($ queryRecords)  )){
 $ data [] = $ row; 
} 
 
 $ json_data = array(
“draw”=&gt;  1,
“recordsTotal”=&gt;  intval($ totalRecords),
“recordsFiltered”=&gt;  intval($ totalRecords),
“data”=&gt;  $ data 
); 
 
echo json_encode($ json_data);  
 
print_r($ json_data); 
?&gt; 
   
 
 

但是,我收到错误

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

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:");
    
    评论
    解决 无用
    打赏 举报