PHP PDO, Ubuntu, MDBTools Problems connecting to Access Database - php

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.

Related

How do I use sphinx search with PHP?

I am still a programming newbie, please keep that in mind.
I installed SphinxSearch on Linux Mint. I followed instructions from a Digital Ocean tutorial. I created a configuration file (sphinx.conf) as follows:
source mySource{
type = mysql
sql_host = localhost
sql_user = root
sql_pass = mypass
sql_db = test
sql_query = SELECT id, uname FROM users
sql_attr_uint = id
sql_attr_string = uname
sql_port = 3306
}
index test{
source = mySource
path = /var/lib/sphinxsearch/data
docinfo = extern
}
searchd{
listen = 9306:mysql41
log = /var/log/sphinxsearch/searchd.log
query_log = /var/log/sphinxsearch/query.log
read_timeout = 5
pid_file = /var/run/sphinxsearch/searchd.pid
seamless_rotate = 1
preopen_indexes = 1
unlink_old = 1
binlog_path = /var/lib/sphinxsearch/data
}
My PHP File
<?php
$conn = new mysqli('localhost','root','mypass','test',9306);
if($conn->connect_error){
die("Could not connect");
}else{
echo "You are connected!";
}
$query = $conn->query("SELECT * FROM test WHERE MATCH('hsmith')");
echo $query->error;
?>
PHP is throwing an error stating I am using a "Non-Object"
Any solution? Am I doing somethin entirely wrong?
Most likely the problem is in
$conn = new mysqli('localhost','root','mypass','test',9306);
As per http://php.net/manual/en/mysqli.construct.php when you pass 'localhost' in the 1st param mysqli tries to use unix socket, not a TCP socket and the port number therefore gets just ignored, i.e. you probably connect to your mysql instance instead of sphinx which then causes the problem you get.
Try:
$conn = new mysqli('127.0.0.1','','','test',9306);
instead.
Please also be aware that
echo $query->error;
is wrong since mysqli_result (http://php.net/manual/ru/class.mysqli-result.php) returned by query() does not have property 'error', you probably meant
echo $conn->error;

How to acces .mdb and return value from it using php

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

ADOdb executing query don't work

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

MDB access using php

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);

Connecting to Oracle with PHP on IIS

I'm having all sorts of trouble...
Here is the code I'm using:
$c = OCILogon('user', 'pass', 'host');
I get the following error:
PHP Warning: ocilogon(): ociopen_server: Error while trying to retrieve text for error ORA-12514 in D:\Inetpub\wwwroot**\oracle.php on line 26
Anyone know what the hell I'm doing wrong?
It's PHP4, IIS6 btw. I've tried this on PHP5, IIS7 as well, no luck.
Thanks for any help I can get... :(
You must have correctly configured TNSNAMES.ora file, where is stored information about connection to database. Oracle errorr ORA-12514 says:
TNS:listener does not currently know
of service requested in connect
descriptor
Function OCILogon have this syntax (I'am not PHP developer, so excuse me if I was not right):
resource oci_connect ( string
$username , string $password [,
string $connection_string [, string
$character_set [, int $session_mode
]]] )
In your example is on third position parametr "host". But manual says "connectin string".
This "connection string" must be coonfigured throught file $ORACLE_HOME/network/admin/tnsnames.ora file ($ORACLE_HOME is folder where is Oracle client installed).
TNSNAMES.ORA look like this(example):
TEST_DB = (DESCRIPTION =(ADDRESS_LIST
=(ADDRESS = (COMMUNITY = tcp.world)(PROTOCOL = TCP)(Host =
127.0.0.1)(Port = 1521)))(CONNECT_DATA = (SID = TESTDB_SID)))
Instead:
$c = OCILogon('user', 'pass', 'host');
You should use:
$c = OCILogon('user', 'pass', 'TEST_DB');
...TEST_DB is service name from tnsnames.ora file
And yet for complementing (my file $ORACLE_HOME/network/admin/sqlnet.ora look like this):
SQLNET.AUTHENTICATION_SERVICES= (NTS)
NAMES.DIRECTORY_PATH= (TNSNAMES, EZCONNECT)
NAME.DEFAULT_ZONE = world
NAMES.DEFAULT_DOMAIN = world
And finally PHP manual example (connection string can be inserted directly into variables in PHP):
<?php
$db ="(DESCRIPTION =
(ADDRESS =
(PROTOCOL = TCP)
(HOST = HOSTNAMEHERE)
(PORT = 1521)
)
(CONNECT_DATA = (SID = SIDNAMEHERE))
)";
$odbc = ocilogon ('user', 'pass', $db) or die( "Could not connect to Oracle database!") or die (ocierror());
?>
try using persistant connection oci_pconnect()... worked for me

Categories