0

I have an example that works perfectly in mysql but when I converts to postgresql don't works.

Mysql Code: (index.php):

<?php 
  require('includes/connection.php'); 
  include('func.php');
?>
<html>
<head>
</head>

<body>
<p>
<form action="" method="post">

    <select name="drop_1" id="drop_1">

      <option value="" selected="selected" disabled="disabled">Select a Category</option>

      <?php getTierOne(); ?>

    </select> 

</form>

</body>
</html>

Mysql code(func.php):

function getTierOne()
{       

        $result = mysql_query("SELECT DISTINCT tier_one FROM three_drops") 
or die(mysql_error());

  while($tier = mysql_fetch_array( $result )) 

    {
       echo '<option value="'.$tier['tier_one'].'">'.$tier['tier_one'].'</option>';
    }
}

POstgreSql Code: (index.php):

<?php 
  require('includes/connection.php'); 
  include('func.php');
?>
<html>
<head>
</head>

<body>
<p>
<form action="" method="post">

    <select name="drop_1" id="drop_1">

      <option value="" selected="selected" disabled="disabled">Select a Category</option>

      <?php getTierOne(); ?>

    </select> 

</form>

</body>
</html>

PostgreSql code(func.php):

function getTierOne()
{       

        $sth = $dbh->query("SELECT DISTINCT tier_one FROM three_drops");

      while($tier = $sth->fetch()) 

        {
           echo '<option value="'.$tier['tier_one'].'">'.$tier['tier_one'].'</option>';
        }
}

Connections (Postgresql):

require("constants.php");

    $dbh = new PDO('pgsql:host=' . DB_SERVER . ';dbname=' . DB_NAME, DB_USER, DB_PASS,
    array (PDO::ATTR_PERSISTENT => true ));
    $dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );

The connection is working but I get an error: Call to a member function query() on a non-object.

2
  • Should the $dbh object be initialized somewhere because now it isn't? What do you have in connection.php? Maybe just replace $dbh->query with pg_query and $sth->fetch with pg_fetch_assoc($sth)? Commented Jul 25, 2011 at 10:16
  • I update my question with the connection. I try your suggestion but I have the same result: Call to a member function pg_query() on a non-object. Commented Jul 25, 2011 at 10:26

1 Answer 1

1

Try defining the $dbh global.

connection.php:

require("constants.php");
global $dbh;

func.php:

function getTierOne()
{
  global $dbh;
Sign up to request clarification or add additional context in comments.

2 Comments

Don't works, I get the same problem.If I only select and echo the row outside the function works fine, but inside the select box nothing happens.
Ah, sorry. The global $dbh; needs to be inside the function. Edited.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.