Solved

No results for php/postgresql query

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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Suggested Solutions

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
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.

777 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