Hello everyone, I am trying to retrieve some data from PostgreSQL using jQuery and PHP. I should be able to enter a date as input -e.g, as 2010-08-09 and retrieve all the data that is dated August 9th- however, when I execute my javascript, all I get as an output is this:

"; echo "
".$rows." -by the way, misc.sp_archive is the sql table, timestamp is the date element of table-
"; } ?>

My code is as below


Javascript Part

<head>
   <style type="text/css">
   #age{
		display: none;
		line-height: 0px;
		font-size: 20px;
	}
	</style>
   
   
   <script type="text/javascript" src="jquery-1.4.2.js"></script>
   <script type="text/javascript">
   
		function getDates(value){
		$.post("file.php",{myDate:value},function(data) {
		$("#results").html(data);

});}
   
   
   </script>
   
   
   
 </head>
 <body>
 
	<input type="text" onkeyup="getDates(this.value)"/>
	<br>
	<div id="results"></div>
 
 </body>
 </html>

and here is the php code

<?php
$myDate=$_POST['myDate'];
$connection = pg_connect("dbname=mydbname user=tehusername host=localhost port=5432");
$myresult = pg_query($connection, "SELECT * FROM misc.sp_archive WHERE misc.sp_archive.timestamp BETWEEN '$myDate' AND '2010-08-12' LIMIT 15");
while($rows = pg_fetch_all($myresult))
{	
	echo "<pre>";
	echo "<div>".$rows['misc.sp_archive.timestamp']."</div>";
}
?>

How can I get this code to work ? I double checked everything and it looks fine as far as I know. I also checked whether I am connected to the database during the code execution and I am. I would be grateful for any help. Thanks.

pg_fetch_all returns an array, so you should do:

$rows = pg_fetch_all($myresult);
foreach ($rows as $row) {
  echo $row['misc.sp_archive.timestamp'] . "<br/>";
  // echo $row['sp_archive.timestamp'] . "<br/>"; // it could be this line instead
}

pg_fetch_all returns an array, so you should do:

$rows = pg_fetch_all($myresult);
foreach ($rows as $row) {
  echo $row['misc.sp_archive.timestamp'] . "<br/>";
  // echo $row['sp_archive.timestamp'] . "<br/>"; // it could be this line instead
}

Hello there, thanks for your reply but I still have the same problem. Still no clues to why it acts like that..

Don't use database/table name in displaying result

while($rows = pg_fetch_all($myresult))
{	
	echo "<pre>";
	echo "<div>".$rows['timestamp']."</div>";
}

Don't use database/table name in displaying result

while($rows = pg_fetch_all($myresult))
{	
	echo "<pre>";
	echo "<div>".$rows['timestamp']."</div>";
}

Just tried that to no avail. Nevertheless, thank you for your reply, I will continue to tinker with the code.

Don't use database/table name in displaying result

while($rows = pg_fetch_all($myresult))
{	
	echo "<pre>";
	echo "<div>".$rows['timestamp']."</div>";
}

Just realized that I was not executing the file via the EasyPHP server from 127.0.0.1 but instead the easy php www folder. Now it works, thanks !

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.