Solved

No results for php/postgresql query

Posted on 2009-05-15
3
411 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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

615 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