By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,022 Members | 856 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,022 IT Pros & Developers. It's quick & easy.

PHP Access Database Auto Number Issue

P: 52
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.
Dec 6 '07 #1
Share this Question
Share on Google+
4 Replies


nathj
Expert 100+
P: 938
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
Dec 6 '07 #2

P: 52
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
Dec 10 '07 #3

P: 41
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?
Dec 10 '07 #4

nathj
Expert 100+
P: 938
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
Dec 11 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.