PHP Access Database Auto Number Issue

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • movieking81
    New Member
    • Feb 2007
    • 52

    PHP Access Database Auto Number Issue

    Hello All-

    I need help on a Select statement. I have this...

    Code:
    <?php
    
    $conn=odbc_connect('databasename','','');
    if (!$conn)
      {exit("Connection Failed: " . $conn);}
      
    	$sql="SELECT * FROM tablename where id = '" . $_REQUEST['id'] . "'";
    
    	echo "$sql";
    	$rs=odbc_exec($conn,$sql);
    ?>
    When I run the page I get this...

    Code:
    PHP Warning: odbc_exec() [function.odbc-exec]: SQL error: [Microsoft][ODBC Microsoft Access Driver] Data type mismatch in criteria expression., SQL state 22005 in SQLExecDirect in C:\yada\yada\yada.php on line 27
    What am I doing wrong? I have printed the Select statement and it does get a value like so...

    Code:
    SELECT * FROM tablename where id = '99999'
    Do I have to wrap a convert function around the requested id? I'm not really a PHP person, I concentrate mainly on ASP so I'm a little confused. Do I have the syntax correct? Any help would be great.

    Also, as far as the Access Database goes the "id" field is set as "Autonumber " to increment and is the primary key of the table.
  • nathj
    Recognized Expert Contributor
    • May 2007
    • 937

    #2
    Originally posted by movieking81
    Hello All-

    I need help on a Select statement. I have this...

    Code:
    <?php
    
    $conn=odbc_connect('databasename','','');
    if (!$conn)
      {exit("Connection Failed: " . $conn);}
      
    	$sql="SELECT * FROM tablename where id = '" . $_REQUEST['id'] . "'";
    
    	echo "$sql";
    	$rs=odbc_exec($conn,$sql);
    ?>
    When I run the page I get this...

    Code:
    PHP Warning: odbc_exec() [function.odbc-exec]: SQL error: [Microsoft][ODBC Microsoft Access Driver] Data type mismatch in criteria expression., SQL state 22005 in SQLExecDirect in C:\yada\yada\yada.php on line 27
    What am I doing wrong? I have printed the Select statement and it does get a value like so...

    Code:
    SELECT * FROM tablename where id = '99999'
    Do I have to wrap a convert function around the requested id? I'm not really a PHP person, I concentrate mainly on ASP so I'm a little confused. Do I have the syntax correct? Any help would be great.

    Also, as far as the Access Database goes the "id" field is set as "Autonumber " to increment and is the primary key of the table.
    Hi,

    Try the following code:

    [php]
    <?php

    $conn=odbc_conn ect('databasena me','','');
    if (!$conn)
    {exit("Connecti on Failed: " . $conn);}

    $lnID = $_REQUEST['id'] ;
    $sql="SELECT * FROM tablename where id = $lnID";

    echo "$sql";
    $rs=odbc_exec($ conn,$sql);
    ?>
    [/php]
    That works for me in MySQL and I beleive it should work in Access also.

    Cheers
    nathj

    Comment

    • movieking81
      New Member
      • Feb 2007
      • 52

      #3
      Works Great, thanks! But why do I have to create a variable? Why can't I put the "$_Request("wha tever")" right in the select.

      Thanks

      Comment

      • clai83
        New Member
        • Dec 2007
        • 41

        #4
        Originally posted by movieking81
        Works Great, thanks! But why do I have to create a variable? Why can't I put the "$_Request("wha tever")" right in the select.

        Thanks
        Did you try the SQL statement without the single quotes?

        Comment

        • nathj
          Recognized Expert Contributor
          • May 2007
          • 937

          #5
          Originally posted by movieking81
          Works Great, thanks! But why do I have to create a variable? Why can't I put the "$_Request("wha tever")" right in the select.

          Thanks
          Hi,

          First of all I'm gald that it worked for you.

          Second the reason I suggested the variable was two fold; first I like to have a copy of the information so that if it changes I still have the original for comparison. This may not be relevant in your case but for me I've found it useful. Second, and proibably more significant is that I have never tried it the way you suggest so it might work just fine.

          Third, I notice you use $_REQUEST[]. At the risk of sticking my nose in where it's not wanted I would recommend not using this array. The data in there comes from $_POST or $_GET. So I think it always better to be explicit in the code as it make it easier to debug later. You will always know where the information came from just be reading that line.

          Anyway, all the best with the rest of your project.

          Cheers
          nathj

          Comment

          Working...