I try to connect to datebase with odbc_mssql driver:
$kpp_db = ADONewConnection('odbc_mssql');
$kpp_db->debug = true;
$kpp_db->charSet="UTF-8";
$kpp_dbDSN = "Driver={SQL Server};Server=$kppConfig_dbHost;Database=$kppConfig_dbName;";
$kppConfig_akEnabled=$kpp_db->Connect($kppConfig_dbDSN, $kppConfig_dbLogin, $kppConfig_dbPassword);
Then i try to execute some query:
kpp_db->Exequte($quer);
In result i get the message 'Missing extension for odbc'. What can be wrong?
UPD: I install php5-odbc package and now message is
IM002: [unixODBC][Driver Manager]Data source name not found, and no default driver specified
Related
I am using odbc_connect() to connect php to .dbf files, i tried adding "Microsoft dBase Drive (*.dbf)" to my System DNS. although the connection was successful from my code I cannot run the odbc_exe() to get the data, it show "External table is not in the expected format"
//Storing DSN(Data Source Name created)
$dsn="myDBFdbDSNConnection";
$user="";
$password="";
$conn=odbc_connect($dsn,$user, $password);
if (!$conn)
{
echo (die(odbc_error()));
}
else
{
echo "Connection Successful !";
$sql="SELECT * FROM table1";
$rs=odbc_exec($conn,$sql);
}
//Resource releasing
odbc_close($conn);
I am having trouble debugging a connection to an MSAccess 2010 .accdb file.
I'm running a Scotch Box Vagrant vm (v3.5) and have installed the MDBTools odbc driver via sudo apt-get install mdbtools.
My /etc/odbcinst.ini file looks like this:
# To enable unixODBC tracing, add this section to odbcinst.ini.
# Include the [ODBC] section heading.
[ODBC]
Trace = yes
TraceFile = /var/www/unixODBC.log
[MDBTools]
Description=MDBTools Driver
Driver=/usr/lib/x86_64-linux-gnu/odbc/libmdbodbc.so
Setup=/usr/lib/x86_64-linux-gnu/odbc/libmdbodbc.so
FileUsage=1
UsageCount=1
I've also created a datasource in /etc/odbc.ini
[CONTACTS]
Driver = MDBTools
Description = Contacts Database
Database = /data/contacts-db.accdb
Server = localhost
The test php file I'm using to attempt a connection is called testMSACCESS.php and looks like this:
<?php
try {
$dbFile = '/data/contacts-db.accdb';
echo 'Does file exist at ' . $dbFile . '? ';
var_dump(file_exists($dbFile));
var_dump(file_exists($dbFile));
echo 'Is the file readable ? ';
var_dump(is_readable($dbFile));
echo 'Is the file writable ? ';
var_dump(is_writable($dbFile));
$dbh = new PDO("odbc:Driver=MDBTools;DBQ={$dbFile};");
// or $dbh = new PDO("odbc:Driver=MDBTools;DSN=CONTACTS;");
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
if ($dbh){
echo "success <br/>";
}
$dbh = null;
}catch (Exception $e){
echo '<pre>';
echo 'Exception caught: could not connect to database.' . PHP_EOL;
echo (string) $e;
echo '</pre>';
}
The db file, contacts-db.accdb, is in a dir at the root of the machine /data.
When I try to connect by loading the script in a browser, the Exception thrown looks like this:
Does file exist at /data/contacts-db.accdb? bool(true)
Is the file readable ? bool(true)
Is the file writable ? bool(true)
Exception caught: could not connect to database.
PDOException: SQLSTATE[00000] SQLDriverConnect: 0 in /var/www/public/testMSACCESS.php:6
Stack trace:
#0 /var/www/public/testMSACCESS.php(6): PDO->__construct('odbc:Driver=MDB...')
#1 {main}
When running the script via the cmd line, I get a bit more info:
vagrant#scotchbox:~$ php /var/www/public/testMSACCESS.php
Does file exist at /data/contacts-db.accdb? bool(true)
Is the file readable ? bool(true)
Is the file writable ? bool(true)
Unknown Jet version.
File not found
Unable to locate database /data/contacts-db.accdb
<pre>Exception caught: could not connect to database.
PDOException: SQLSTATE[00000] SQLDriverConnect: 0 in /var/www/public/testMSACCESS.php:6
Stack trace:
#0 /var/www/public/testMSACCESS.php(6): PDO->__construct('odbc:Driver=MDB...')
#1 {main}</pre>
And this is what the unixODBC trace log looks like:
[ODBC][1515][1536073973.983218][__handles.c][460]
Exit:[SQL_SUCCESS]
Environment = 0x5591139d5d60
[ODBC][1515][1536073973.985108][SQLSetEnvAttr.c][189]
Entry:
Environment = 0x5591139d5d60
Attribute = SQL_ATTR_ODBC_VERSION
Value = 0x3
StrLen = 0
[ODBC][1515][1536073973.985588][SQLSetEnvAttr.c][363]
Exit:[SQL_SUCCESS]
[ODBC][1515][1536073973.990811][SQLSetEnvAttr.c][189]
Entry:
Environment = 0x5591139d5d60
Attribute = SQL_ATTR_CP_MATCH
Value = (nil)
StrLen = 0
[ODBC][1515][1536073973.991928][SQLSetEnvAttr.c][363]
Exit:[SQL_SUCCESS]
[ODBC][1515][1536073973.992707][SQLAllocHandle.c][375]
Entry:
Handle Type = 2
Input Handle = 0x5591139d5d60
[ODBC][1515][1536073973.993173][SQLAllocHandle.c][493]
Exit:[SQL_SUCCESS]
Output Handle = 0x5591139d6400
[ODBC][1515][1536073973.993682][SQLSetConnectAttr.c][396]
Entry:
Connection = 0x5591139d6400
Attribute = SQL_ATTR_AUTOCOMMIT
Value = 0x1
StrLen = -6
[ODBC][1515][1536073973.994129][SQLSetConnectAttr.c][681]
Exit:[SQL_SUCCESS]
[ODBC][1515][1536073973.994892][SQLSetConnectAttr.c][396]
Entry:
Connection = 0x5591139d6400
Attribute = SQL_ATTR_ODBC_CURSORS
Value = (nil)
StrLen = -6
[ODBC][1515][1536073973.995351][SQLSetConnectAttr.c][681]
Exit:[SQL_SUCCESS]
[ODBC][1515][1536073973.995872][SQLDriverConnect.c][726]
Entry:
Connection = 0x5591139d6400
Window Hdl = (nil)
Str In = [Driver=MDBTools;DBQ=/var/www/public/contacts-db.accdb;][length = 54]
Str Out = 0x7ffdb1be6d00
Str Out Max = 1023
Str Out Ptr = 0x7ffdb1be6cf8
Completion = 0
UNICODE Using encoding ASCII 'ISO8859-1' and UNICODE 'UCS-2LE'
[ODBC][1515][1536073974.001199][SQLDriverConnect.c][1353]
Exit:[SQL_ERROR]
[ODBC][1515][1536073974.001731][SQLGetDiagRec.c][680]
Entry:
Connection = 0x5591139d6400
Rec Number = 1
SQLState = 0x7f11dce6c010
Native = 0x7f11dce6c218
Message Text = 0x7f11dce6c016
Buffer Length = 511
Text Len Ptr = 0x7ffdb1be688a
[ODBC][1515][1536073974.002167][SQLGetDiagRec.c][717]
Exit:[SQL_NO_DATA]
[ODBC][1515][1536073974.002707][SQLEndTran.c][421]
Entry:
Connection = 0x5591139d6400
Completion Type = 1
[ODBC][1515][1536073974.003125][SQLEndTran.c][433]Error: 08003
[ODBC][1515][1536073974.003551][SQLDisconnect.c][208]
Entry:
Connection = 0x5591139d6400
[ODBC][1515][1536073974.004006][SQLDisconnect.c][237]Error: 08003
[ODBC][1515][1536073974.004462][SQLFreeHandle.c][284]
Entry:
Handle Type = 2
Input Handle = 0x5591139d6400
[ODBC][1515][1536073974.004999][SQLFreeHandle.c][333]
Exit:[SQL_SUCCESS]
[ODBC][1515][1536073974.005544][SQLFreeHandle.c][219]
Entry:
Handle Type = 1
Input Handle = 0x5591139d5d60
I can see where the failure is happening in the trace, [SQLDriverConnect.c][726] and [SQLDriverConnect.c][1353]. It appears to be a connection error, but I cannot make sense of the information contained in the following block in the trace log, [SQLGetDiagRec.c][680].
Also, I don't understand why the database can't be found if PHP can find it or why it would say it doesn't know the Jet Engine version since the file was created with an 8 year old version of MS Access (2010).
If anyone has any ideas as to how to debug the error I would greatly appreciate it.
I have connect Ms Access Database to PHP File. PHP file Give Error
"Warning: odbc_connect(): SQL error: [Microsoft][ODBC Driver Manager]
Data source name not found and no default driver specified, SQL state IM002 in SQLConnect in C:\wamp\www\PI\Connection.php on line 3".
Connection.php
<?php
$con = odbc_connect("PIInstitute","","");
if($con){
echo "Connected";
}else{
echo "Failed";
}
?>
You need to specify your driver when calling odbc_connect() like so:
$conn = odbc_connect ( "Driver={SQL Server};Server=$servername;Database=$dbname;", $username, $password ) or die ( "Connection failed: " . $conn );
You can find more info on odbc_connect()here: http://php.net/manual/en/function.odbc-connect.php
The connection id returned by this functions is needed by other ODBC functions. You can have multiple connections open at once as long as they either use different db or different credentials.
resource odbc_connect ( string $dsn , string $user , string $password [, int $cursor_type ] )
<?php
// Microsoft SQL Server using the SQL Native Client 10.0 ODBC Driver - allows connection to SQL 7, 2000, 2005 and 2008
$connection = odbc_connect("Driver={SQL Server Native Client 10.0};Server=$server;Database=$database;", $user, $password);
// Microsoft Access
$connection = odbc_connect("Driver={Microsoft Access Driver (*.mdb)};Dbq=$mdbFilename", $user, $password);
// Microsoft Excel
$excelFile = realpath('C:/ExcelData.xls');
$excelDir = dirname($excelFile);
$connection = odbc_connect("Driver={Microsoft Excel Driver (*.xls)};DriverId=790;Dbq=$excelFile;DefaultDir=$excelDir" , '', '');
?>
I've a mdb file called short_db.mdb in my server which is placed under wwwroot\db\short_db.mdb,I have created a sample php script called fetchmdb.php to fetch values from aforementioned mdb i.e follows
<?php
$count =0;
$db_path = "short_db.mdb ";
$odbc_con = new COM("ADODB.Connection") or die("Cannot start ADO");
$constr = "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" . $db_path . ";";
$odbc_con -> open($constr); //line - 9
$sql = "SELECT top 5 `id`
,val1
,val2
FROM `tbl_accounts`";
echo $sql;
$odbc_con -> execute ($sql);
$rs = null;
$conn = null;
echo "done";
?>
and this fetchmdb.php is also hosted into the same place where mdb file is located (wwwroot\db\fetchmdb.php) so both mdb and php files are under in one directory(wwwroot\db)
So, When I run my url(http://thesite.com/db/fetchmdb.php) getting following error
Fatal error: Uncaught exception 'com_exception' with message 'Source:
Microsoft OLE DB Provider for ODBC Drivers Description:
[Microsoft][ODBC Microsoft Access Driver] Could not use '(unknown)';
file already in use.' in
E:\HostingSpaces\medlabsi\remedyonline.in\wwwroot\db\mdb.php:9 Stack
trace: #0 E:\HostingSpaces\mysite\thesite.com\wwwroot\db\mdb.php(9):
com->open('DRIVER={Microso...') #1 {main} thrown in
E:\HostingSpaces\mysite\thesite.com\wwwroot\db\mdb.php on line 9
Fixed the issue using this solution
I am having a little bit of trouble trying to write to a .mdb database from php.
The database has a table called comments and it has 3 columns "id", "comment" and "by". The code I have is this.
<?php
$count =0;
$db_path = "newsBlog.mdb";
$odbc_con = new COM("ADODB.Connection");
$constr = "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" . $db_path . ";";
$odbc_con -> open($constr);
$sql = "INSERT INTO [comments] VALUES (".$_POST['newsId'].",'".$_POST['comment']."', '".$_POST['by']."')";
echo $sql;
$odbc_con -> execute (sql);
$rs = null;
$conn = null;
echo "done";
?>
It takes 3 values from a form on the previous page and should insert them into the database, except I get this error every time:
<b>Fatal error</b>: Uncaught exception 'com_exception' with message '<b>Source:</b> Microsoft OLE DB Provider for ODBC Drivers<br/><b>Description:</b> [Microsoft][ODBC Microsoft Access Driver] Invalid SQL statement; expected 'DELETE', 'INSERT', 'PROCEDURE', 'SELECT', or 'UPDATE'.' in D:\Hosting\7010757\html\users\kinectfashion\postComment.php:20 Stack trace:
#0 D:\Hosting\7010757\html\users\kinectfashion\postComment.php(20): com->execute('sql')
#1 {main} thrown in <b>D:\Hosting\7010757\html\users\kinectfashion\postComment.php</b> on line <b>20</b><br />
To me, this seams MAD as I am using an insert statement and cannot see what I am doing wrong!
Any help would be much appreciated!
Thanks
$odbc_con -> execute (sql); should be $odbc_con -> execute ($sql);