2016-12-27 159 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>