Solved

No results for php/postgresql query

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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
splitting a sting into elements 6 75
How difficult would it be build a cookie scanner in PHP? 21 59
Postgresql File 4 21
e commerce steps shown instead of self testing 2 58
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…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Steps to create a PostgreSQL RDS instance in the Amazon cloud. We will cover some of the default settings and show how to connect to the instance once it is up and running.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

739 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