Hi guys

I am using the below code to connect to a run a query on a microsoft access database. when i run the code i get the error:

Fatal error: Call to a member function Execute() on a non-object in C:\web\root\devfiles\accesstest.php on line 27

i cant see why i would be getting the error any halp would be aprishated.


<?php

$dbc = new COM("ADODB.Connection");
$conn = "DRIVER=Microsoft Access Driver (*.mdb); DBQ=".$_SERVER['DOCUMENT_ROOT']."/membership.mdb"; //was $connstr
$dbc->open($conn);        //was $connstr


$sql = 'SELECT   forename, surname
	FROM     members';
$rs = $conn->Execute($sql);

while (!$rs->EOF) { 
?>
	<tr>
		<?php echo $rs->Fields['forename']->Value ?>
		<?php echo $rs->Fields['surname']->Value ?>

</tr>
<?php $rs->MoveNext() ?>
<?php } ?>

    May be checking the result return by

    $dbc->open($conn); 

    looking for any error ?

      sorry how would i do that? i know the code is lacking error ehecking but am unsure how to include it

        I have included error checking and the result is that the "can not open connection to db" line is displaied. I dont know why this is, do i need to change anything in the php.ini to say i want to use adodb connections? otherwise if you can see why i can not connect to the database please please help.

        
        <?php
        
        $dbc = new COM("ADODB.Connection") or die("cannot start ADO");
        
        //access connection string
        $conn = ("DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=C:\\web\\root\\membership.mdb"); //was $connstr
        
        
        $dbc->open($conn) or die ("can not open connection to db");        //was $connstr
        
        
        $sql = 'SELECT   forename, surname
        	FROM     members';
        $rs = $conn->Execute($sql);
        
        while (!$rs->EOF) { 
        ?>
        	<tr>
        		<?php echo $rs->Fields['forename']->Value ?>
        		<?php echo $rs->Fields['surname']->Value ?>
        
        </tr>
        <?php $rs->MoveNext() ?>
        <?php } ?>
        
        

          May be you need some login/password ? Or the PHP program need write access to
          the directory where the MDB database is ? (to create *.ldb file)

          $sql = "DRIVER={Microsoft Access Driver (*.mdb)} ;
                         DBQ=". realpath($db) ." ;
                         uid=admin ;
                         pwd=$pass ;" ; 
          

          Otherwise, I don't know if you need something special in php.ini, but I'm not familiar
          with such connections to Windows database.

          Some explanations here : http://fr2.php.net/manual/en/ref.com.php

          If the PHP script is not on the same machine as the database, I think you should
          enable "com.allow_dcom" in php.ini .

          (another direction if you're completely stuck here is to use ODBC instead of
          ADOD😎

            I've got a feeling it is the double slashes in your db path that is throwing this - I don't see why you would need to escape the slashes since COM does not process them as escape chars in the first place.

              cheers for the advice guys i will se how i get on and report back. Im not opposed to using ODBC instead as i just really need to get this connection working .

                hi guys i removed the double slashes and the code is still giving the 'or die' message on the conn-> open line

                i am not aware opf setting up a password or username on the database would the connection code still be waiting for one even if i havent set it?

                any further ideas would be much aprishiated here is the code i have at the moment:

                <?php
                
                $conn = new COM("ADODB.Connection") or die("cannot start ADO");
                
                //access connection string
                $conn->open("DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=C:\web\membership.mdb") or die ("can not open connection to db"); //was $connstr
                
                
                $sql = 'SELECT   forename, surname
                	FROM     members';
                $rs = $conn->Execute($sql);
                
                while (!$rs->EOF) { 
                ?>
                	<tr>
                		<?php echo $rs->Fields['forename']->Value ?>
                		<?php echo $rs->Fields['surname']->Value ?>
                
                </tr>
                <?php $rs->MoveNext() ?>
                <?php } ?>
                
                

                  No, I don't think the connection code will wait for a login/password if you haven't set it up.

                  Is the database on the same machine as the PHP code ?

                  Other idea :

                  • replacing c:...... by c:/.../... ?

                  • add a ';' at the end ?

                  instead of having :

                  "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=C:\web\membership.mdb"

                  put :

                  "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=C:\web\membership.mdb[color=red];[/color]"

                  ?

                    Access so I guess that it is IIS on W2k or W2003. That put you into the realms of NTFS file permissions and NT user permissions. Now every browser, and hence every invocation of a php script by a browser, will default to an anonymous user with no NT permissions beyond 'read'. I forget what the IIS anon user name is. You can either extend the anon user's permisions or give EVERYONE read/write/execute permisions on the folder the db is in.

                      Write a Reply...