Solved

No results for php/postgresql query

Posted on 2009-05-15
3
406 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
  • 2
3 Comments
 
LVL 34

Accepted Solution

by:
Beverley Portlock earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
Thank you! Two excellent tips, the second probably even more valuable.
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
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 is …
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.

728 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now