2016-12-27 157 views
0

我已經創建了一個Php文件,它從Postgres數據庫中獲取數據,但是當我運行該文件時,它不顯示數據庫表中存在的內容(記錄)。無法使用Php從postgress數據庫中檢索數據?

我使用Apache服務器和Python SimpleHTTPServer也運行此。

我也重新啓動了Postgres服務器。

This is the Output which I get

下面是該文件的代碼:

的index.php

<!DOCTYPE html> 
<html> 
    <head> 
     <title> 
      webpage 
     </title> 
    </head> 
    <body> 
     <h1> 
      INFORMATION OF DATABASE 
     </h1> 
     <?php 
$host = "localhost"; 
$user = "myappuser"; 
$pass = "password"; 
$db = "myapp"; 
echo "\n test"; 
$con = pg_connect("host=$host dbname=$db user=$user password=$pass") 
    or die ("Could not connect to server\n"); 
$query = "SELECT * FROM app1_snippet"; 
$rs = pg_query($con, $query) or die("Cannot execute query: $query\n"); 
while ($row = pg_fetch_assoc($rs)) { 
    echo $row['id'] . " " . $row['name'] . " " . $row['phone_no']. " " . $row['status']; 
    echo "\n"; 
} 
pg_close($con); 
     ?> 
    </body> 
</html> 
+0

它可以幫助'的print_r(CON); pg_connect後''權()'然後'的print_r(RS);'pg_query後'()'所以它變得更加明顯,你是否已經實際連接和執行查詢而不是你的表是空的。 –

+1

甚至連接之前的echo命令似乎不起作用,因此它不是postgres錯誤。你得到一個500,打開調試,看看有什麼錯誤。 –

+0

@anton samsonov我已經這樣做,但它沒有顯示任何東西 –

回答

0

雅想通了怎麼辦。下面是代碼 。

<!DOCTYPE html> 
<html> 
<head>WELCOME</head> 
<body style="background-color:#E4E5E7"> 
<style> 
table, th, td { 
    border: 1px solid black; 
    border-collapse: collapse; 
} 
th, td { 
    padding: 5px; 
    text-align: left; 
} 
table#t01 { 
    width: 100%;  
    background-color: #f1f1c1; 
} 
</style> 
</head> 
<body> 


<?php 
$url= 'http://yoururltypehere.com'; 
$options = array(
    'http' => array(
    'header' => array(
        'name: '.$_GET['name'], 
       ), 
    'method' => 'GET', 
), 
); 
$context = stream_context_create($options); 
$output = file_get_contents($url, false,$context); 

$arr = json_decode($output,true); 


?> 


<table style="width:100%"> 
    <tr> 
    <th>ID</th> 
    <th>NAME</th> 
    <th>PHONE_NO</th> 
    <th>STATUS</th> 
    </tr> 

    <br> 
     <?php 
    for($x=0;$x<count($arr);$x++) 
    { 
       ?> 
    <tr> 
     <td><?php echo $arr[$x]['id']; ?> 

    <td><?php echo $arr[$x]['name']; ?> 
    <td><?php echo $arr[$x]['ph_no']; ?> 
    <td><?php echo $arr[$x]['stats']; ?> 
    </tr> 
     <?php 

       } 
       ?> 

    <br> 
</table> 
</body> 
</html>