为什么这适用于Mysql而不是Postgres

时间:2012-05-16 08:18:38

标签: php ajax postgresql

我正在忙着学习Ajax,并且需要将Postgres用于支持的数据库。我找到了一个有效的Mysql示例,但是当我将其转换为Postgres时,它停止工作。从Mysql交换到Postgres时,我没有对HTML代码进行任何更改。

当我在命令行(php test.php)执行php代码时,它会工作并输出正确数量的行和数据。

这是php

<?php 

$dbh = pg_connect("host=192.168.0.8 dbname=test user=test password=test");
if (!$dbh) {
    die("Error in connection: " . pg_last_error());
}       

// execute query
$sql = "SELECT * FROM users";
$result = pg_query($dbh, $sql);
if (!$result) {
 die("Error in SQL query: " . pg_last_error());
}       

// iterate over result set
// print each row
while ($row = pg_fetch_array($result)) {
   echo "Name: " . $row[1];
    echo "<BR />";
}       

// free memory
pg_free_result($result);       

// close connection
pg_close($dbh);
?>

这是html

<html>
<head>
    <script language="JavaScript" type="text/javascript">
    function ajax_post(){
        // Create our XMLHttpRequest object
        var hr = new XMLHttpRequest();
        // Create some variables we need to send to our PHP file
    var url = "test.php";
    var fn = document.getElementById("name").value;
    var vars = "name="+fn;
    hr.open("POST", url, true);
    // Set content type header information for sending url encoded variables in the request
    hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    // Access the onreadystatechange event for the XMLHttpRequest object
    hr.onreadystatechange = function() {
            if(hr.readyState == 4 && hr.status == 200) {
                var return_data = hr.responseText;
                document.getElementById("status").innerHTML = return_data;
        }
    }
    // Send the data to PHP now... and wait for response to update the status div
    hr.send(vars); // Actually execute the request
    document.getElementById("status").innerHTML = "processing...";
        document.getElementById("name").value = "";
    }
    </script>
    <script type="text/javascript">
        function setFocus() 
        {
          document.getElementById("name").focus();
        }
    </script>
</head>

<body onload="setFocus()">
    <div>
    <form name="input" action="" onsubmit="ajax_post(); return false;"> 
        Barcode : <input type="text" id="name" size="30"><br />       
    </form>
    </div>
    <div style="background-color: lightyellow; border: 1px solid black; width: 400; height: 300;">
        <div id="status"></div>
    </div>
</body>

我注意到的一件事是,当我使用postgres在命令行运行test.php文件时,新行字符总是附加到数据的末尾。

由于 0

1 个答案:

答案 0 :(得分:1)

安装Firefox和Firebug插件。在Firebug中,您可以方便地观察HTML和PHP页面之间的整个通信,还有一个很棒的JS调试器,包括断点等。您似乎并不清楚错误究竟是什么,但您仍然可以使用这些工具发现错误。

相关问题