2016-12-27 23 views
0

私はPostgresデータベースからデータをフェッチするPhpファイルを作成しましたが、ファイルを実行するとデータベーステーブルにある内容(レコード)は表示されません。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> 
関連する問題