SitePoint Sponsor

User Tag List

Results 1 to 2 of 2

Thread: Access and PHP

  1. #1
    Prolific Blogger silver trophy Technosailor's Avatar
    Join Date
    Jun 2001
    Location
    Before These Crowded Streets
    Posts
    9,446
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Access and PHP

    This has got to be a datbase issue because apparently people get this error in nay language they work with. Me? I'm trying to show someone how to connect to an Access database from PHP. I thought this would be relatively easy. Here is the code...
    PHP Code:
     <?php
     
    //Database connection function
     
    function connect_to_access()
         {
         
    $access_usr='';
         
    $access_pas='';
         
    $access_dsn 'DRIVER={Microsoft Access Driver (*.mdb)};
     DBQ=c:\user.mdb;'
    ;
         if(!
    $cnx odbc_pconnect($access_dsn,$access_usr,$access_pas))
             {
             echo 
    'Could not Connect to Access.';
             return 
    false;
             }
         return 
    $cnx;
         }
     
     
    // Query Function
     
    function query($query,$cnx='')
         {
         if(
    $cnx == '')                         #checks to see if database connection 
             
    {                                 #exists and if not creates one
             
    $cnx connect_to_Access(); 
             } 
             
         
    $sql odbc_prepare($cnx,$query);    #Preps Query for Execution
         
    if (!$result odbc_exec($cnx,$sql))                 #Executes Query and returnd
                
    {                                #any errors
                
    echo "Query Failed"
             }
         return 
    $result;
         }
     
     
    // If Form has not been submitted
     
    if(!isset($_POST['submit']))
         {
         
    // Put HTML form in following. Must include a submit button with name="submit"
         
    ?>
         <form action="auth.php" method="post">
             <table>
                 <tr>
                     <td>First Name:&nbsp;</td>
                     <td><input type="text" name="fname" size="20" maxlength="50" /></td>
                 </tr>
                 <tr>
                     <td>Last Name:&nbsp;</td>
                     <td><input type="text" name="lname" size="20" maxlength="50" /></td>
                 </tr>
                 <tr>
                     <td colspan=2"><input type="submit" name="submit" value="AUTHENTICATE" /></td>
                 </tr>
             </table>
         </form>
         <?
         
    exit;
         }
     else
         {
         
    $cnx connect_to_access();
         if(!
    $sql query("SELECT * 
                         FROM (Employees) 
                         WHERE FirstName = '"
    .$_POST['fname']."' AND
                         LastName = '"
    .$_POST['lname']."'"))
             {
             echo 
    "User is not authenticated";
             exit;
             }
         echo
    "This one is good";
         exit;
         } 
     
    ?>
    Connection works fine. But I am getting this error:
    Code:
     Warning: odbc_exec(): SQL error: [Microsoft][ODBC Microsoft Access Driver] Invalid SQL statement; expected 'DELETE', 'INSERT', 'PROCEDURE', 'SELECT', or 'UPDATE'., SQL state 37000 in SQLExecDirect in C:\Program Files\Apache Group\Apache2\htdocs\auth.php on line 28
      Query FailedUser is not authenticated
    What's going on with the ODBC driver?!
    Aaron Brazell
    Technosailor



  2. #2
    SitePoint Enthusiast
    Join Date
    Apr 2004
    Location
    TX
    Posts
    71
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have NO experience w/PHP, but I noticed your error msg echo's this...

    Query FailedUser is not authenticated

    ...I know the "Query Failed" part is flagged earlier, but the part in blue, by looking @ your code I'm assuming the SQL query itself is failing...
    PHP Code:
    if(!$sql query("SELECT * 
                        FROM (Employees) 
                        WHERE FirstName = '"
    .$_POST['fname']."' AND
                        LastName = '"
    .$_POST['lname']."'")) 
    ...the parens around the Employees table, Access won't recognize those (well, obviously it does, in a bad way). Either take them out, or use brackets around that table name...I'm 90% positive that's an error...unless you've used parens around table names b4??


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •