Solved

No results for php/postgresql query

Posted on 2009-05-15
3
409 Views
Last Modified: 2012-06-27
When I execute the following code, I get a blank page. However, if I remove the complete for-loop, and only display the number of rows (pg_num_rows($result)), I do get the correct value back. I don't see what I'm doing wrong here.

System: Windows Vista, PG8.3, PHP 5.2.9
<?php
$query = "select name from person;";
$result = pg_query($db, $query);
if ($result)
{
	$rows = pg_num_rows($result);
	echo("result: ". $rows);
	// start problem code
	for ($row = 0; $row < $rows; $row++)
	{
		$values = pg_fetch_row($result, $row);
		echo 'Person: ' . implode(' ', $values) . " <br/>\n";
	}
	else
	{
		echo ("Query error: " . pg_last_error($db) );
	} 
	// end problem code
}
?>

Open in new window

0
Comment
Question by:R7AF
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 34

Accepted Solution

by:
Beverley Portlock earned 500 total points
ID: 24394090
The most obvious fault is the ELSE being in the wrong place

<?php
$query = "select name from person;";
$result = pg_query($db, $query);
if ($result)
{
        $rows = pg_num_rows($result);
        echo("result: ". $rows);
        // start problem code
        for ($row = 0; $row < $rows; $row++)
        {
                $values = pg_fetch_row($result, $row);
                echo 'Person: ' . implode(' ', $values) . " <br/>\n";
        }
        // end problem code
}
else
{
          echo ("Query error: " . pg_last_error($db) );
}
?>
0
 
LVL 34

Assisted Solution

by:Beverley Portlock
Beverley Portlock earned 500 total points
ID: 24394109
An additional thought. When testing, add this

error_reporting(E_ALL);

as the first line in a script. I think it is probable that PHP is really complaining about the misplaced ELSE but it does not show errors by default. You may need to edit php.ini and set

display_errors = On

but only on a development machine, I do not recommend it on a LIVE web server.
0
 
LVL 13

Author Closing Comment

by:R7AF
ID: 31581860
Thank you! Two excellent tips, the second probably even more valuable.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
PHP website on Linux - server DNS address could not be found. 18 82
Ajax and PHP 9 53
PHP $_POST vs asp request 4 22
Get data from two MySQL tables 6 21
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Many developers have database experience, but are new to PostgreSQL. It has some truly inspiring capabilities. I have several years' experience with Microsoft's SQL Server. When I began working with MySQL, I wanted a quick-reference to MySQL (htt…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question