Connecting Tech Pros Worldwide Forums | Help | Site Map

PHP Access Database Auto Number Issue

Member
 
Join Date: Feb 2007
Posts: 36
#1: Dec 6 '07
Hello All-

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

Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3. $conn=odbc_connect('databasename','','');
  4. if (!$conn)
  5.   {exit("Connection Failed: " . $conn);}
  6.  
  7.     $sql="SELECT * FROM tablename where id = '" . $_REQUEST['id'] . "'";
  8.  
  9.     echo "$sql";
  10.     $rs=odbc_exec($conn,$sql);
  11. ?>
  12.  
When I run the page I get this...

Expand|Select|Wrap|Line Numbers
  1.  
  2. 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 
  3.  
  4.  
What am I doing wrong? I have printed the Select statement and it does get a value like so...

Expand|Select|Wrap|Line Numbers
  1. SELECT * FROM tablename where id = '99999'
  2.  
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's Avatar
Expert
 
Join Date: May 2007
Location: North Tyneside
Posts: 857
#2: Dec 6 '07

re: PHP Access Database Auto Number Issue


Quote:

Originally Posted by movieking81

Hello All-

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

Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3. $conn=odbc_connect('databasename','','');
  4. if (!$conn)
  5.   {exit("Connection Failed: " . $conn);}
  6.  
  7.     $sql="SELECT * FROM tablename where id = '" . $_REQUEST['id'] . "'";
  8.  
  9.     echo "$sql";
  10.     $rs=odbc_exec($conn,$sql);
  11. ?>
  12.  
When I run the page I get this...

Expand|Select|Wrap|Line Numbers
  1.  
  2. 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 
  3.  
  4.  
What am I doing wrong? I have printed the Select statement and it does get a value like so...

Expand|Select|Wrap|Line Numbers
  1. SELECT * FROM tablename where id = '99999'
  2.  
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_connect('databasename','','');
if (!$conn)
{exit("Connection 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
Member
 
Join Date: Feb 2007
Posts: 36
#3: Dec 10 '07

re: PHP Access Database Auto Number Issue


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

Thanks
Member
 
Join Date: Dec 2007
Posts: 41
#4: Dec 10 '07

re: PHP Access Database Auto Number Issue


Quote:

Originally Posted by movieking81

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

Thanks

Did you try the SQL statement without the single quotes?
nathj's Avatar
Expert
 
Join Date: May 2007
Location: North Tyneside
Posts: 857
#5: Dec 11 '07

re: PHP Access Database Auto Number Issue


Quote:

Originally Posted by movieking81

Works Great, thanks! But why do I have to create a variable? Why can't I put the "$_Request("whatever")" 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
Reply