user1398017
user1398017

Reputation: 199

Why does this work with Mysql but not Postgres

I am busy learning Ajax and need to use Postgres for the backed DB. I found an example for Mysql which works but when I convert it to Postgres, it stops working. I have not made any changes to the HTML code when swapping from Mysql to Postgres.

When I execute the php code at command line (php test.php), it works and outputs the correct number of rows and data.

Here is the 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);
?>

And here is the 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>

One thing I have noticed is that when I run the test.php file at command line with postgres, anew-line character is always appended to the end of the data.

Thanks O

Upvotes: 0

Views: 381

Answers (1)

C. Ramseyer
C. Ramseyer

Reputation: 2382

Install Firefox and the Firebug add-on. In Firebug, you can conveniently watch the whole communication between your HTML and PHP page, and there's also a great JS debugger with breakpoints etc. included. You don't seem to have a good idea what the error exactly is, but you should still be able to spot it with these tools.

Upvotes: 1

Related Questions