Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Hi.. Im a little confused.. I was t

Status
Not open for further replies.

Nightsoft

Programmer
Aug 23, 2001
50
0
0
DK
Hi.. Im a little confused.. I was trying to make a OO code from som simple PHP code. Only thing it does. is to connect to a mdb access file and gets the name..

In non OOP the code works fine.. It is as if it can't connect to the database when im using OOP?

Heres my code :

Code:
<?
  class MyConn
  {
    // Function for opening my database
    function NOpenDB()
    {
      echo(&quot;OOP:<br />&quot;);
      $this = new COM('ADODB.Connection');
      $this->open(&quot;Provider=Microsoft.Jet.OLEDB.4.0; Data Source=&quot; . realpath(&quot;TestDB.mdb&quot;));
    }
 
    // Function to write out data  
    function NWriteData()
    {
      $SQLGetDataQuery = &quot;SELECT * FROM TestTB&quot;;
      $RSData = $this->Execute($SQLGetDataQuery);

      while (!$RSData->EOF):
        echo $RSData->Fields['Navn']->Value;

        $RSData->MoveNext();
      endwhile;

      $RSData->Close();
    }

    // Function to close my database connection
    function NCloseDB()
    {
      $this->Close();
    }
  }

  $objMyConn = new MyConn;

  $objMyConn->NOpenDB();
  $objMyConn->NWriteData();  
  $objMyConn->NCloseDB();
?>
This dosen't work though it's pretty much the same as non oop code.. It dies, if i put Die on the database opening.
But the connection is the &quot;same&quot; as in this code :

Code:
<?
  $MyConn = new COM('ADODB.Connection');
  $MyConn->open(&quot;Provider=Microsoft.Jet.OLEDB.4.0; Data Source=&quot; . realpath(&quot;TestDB.mdb&quot;));
  $SQLGetDataQuery = &quot;SELECT * FROM TestTB&quot;;
  $RSData = $MyConn->Execute($SQLGetDataQuery);

  echo(&quot;Non OOP:<br />&quot;);

  while (!$RSData->EOF):
    echo($RSData->Fields['Navn']->Value . &quot;<br />&quot;);
    $RSData->MoveNext();
  endwhile;

  $RSData->Close();
  $MyConn->Close();
?>

The last one worked.!? 

Anybody got any idea, why it won't connect to the database.?
I've dosn't seem to be able to figure this out myself.. But Ill keep on trying.

Thnx

Machine code Rocks:-)
[URL unfurl="true"]www.nightsoft.dk[/URL]
 
Part of it is probably this line:

$this = new COM('ADODB.Connection');

You need to define an object-internal variable to store the COM object:

[tt]class MyConn
{
var $connection;

// Function for opening my database
function NOpenDB()
{
echo(&quot;OOP:<br />&quot;);
$this->connection = new COM('ADODB.Connection');
$this->connection->open(&quot;Provider=Microsoft.Jet.OLEDB.4.0; Data Source=&quot; . realpath(&quot;TestDB.mdb&quot;));
}
}[/tt]


The rest of your object code will have to be modified to use this internal variable.


Want the best answers? Ask the best questions: TANSTAAFL!!
 
Hi there..

Thnx that helped :)

But why is it nessecary to do this.. Seems to me im just adding the connection to every place where i &quot;this->&quot; .?

..

Thnx

Machine code Rocks:)
 
You have to to store the connection handle somewhere. Objects themselves do not store values, they are containers for things wich can store values.

You needed to provide that thing which could store the value.

Want the best answers? Ask the best questions: TANSTAAFL!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top