Jump to content

Postgres conversion of OO script


bugfreek1

Recommended Posts

All--

 

I'm a relative newbie to Postgres and am working on teaching myself OO coding.  While working through a tutorial, I've run into bumps on a conversion of an OO database connection class from MySql to Postgres.  Specifically, my pg_fetch_array function is failing miserably seemingly no matter what I do.  Any help to complete this conversion so I can continue on my tutorial would be greatly appreciated.

 

The Code:

<?
require_once 'SystemComponent.php';

class DbConnector extends SystemComponent {

var $theQuery;
var $link;

//*** Function: DbConnector, Purpose: Connect to the database ***
function DbConnector(){

// Load settings from parent class
$settings = SystemComponent::getSettings();

// Get the main settings from the array we just loaded
$host = $settings['dbhost'];
       $db = $settings['dbname'];
       $user = $settings['dbusername'];
       $pass = $settings['dbpassword'];

// Connect to the database
 $this->link = @pg_connect($host, $db, $user, $pass);

}

//*** Function: query, Purpose: Execute a database query ***
function query($query) {
        $this->theQuery = $query;
        return @pg_query($query, $this->linkid);
}

//*** Function: getQuery, Purpose: Returns the last database query, for debugging ***
function getQuery() {
return $this->theQuery;
}

//*** Function: getNumRows, Purpose: Return row count, MySQL version ***
function getNumRows($result){
//return mysql_num_rows($result);
return @pg_num_rows($result);

}

function fetchArray($result, $num){
if (!isset($num)) $num = 0;
return @pg_fetch_array($result, $num);
} 
}
?>

 

Thank you in advance for any help.

 

Ben

Link to comment
Share on other sites

I notice you use both $this->link and $this->linkid

 

Apart from that, are you checking that pg_query() returned a resource before calling pg_fetch_array() with it?  If pg_query() fails, it will return false.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.