I am trying to use ODBC with php and I seem to be having some problems. This is my code:
<?php
$connect = odbc_connect("digitallibrary", "root", "");
$sql = "SELECT name FROM books";
$result = odbc_exec($connect, $sql);
echo $result;
while (odbc_fetch_row($result)) {
$name = odbc_result($result, "name");
print("$name\n");
}
odbc_close($connect);
?>
I have also installed mysql odbc connector and added this system DSN:
Datasource Name : digitallibrary
TCP/IP Server: localhost Port 80
user: root
database:digitallibrary
When I run my code I get this warnings but nothing printed:
Warning: odbc_connect(): SQL error: [Microsoft][ODBC Driver Manager] The specified DSN contains an architecture mismatch between the Driver and Application, SQL state IM014 in SQLConnect in E:\Program Files\xampp\htdocs\DigitalLibrary\index.php on line 9
Warning: odbc_exec() expects parameter 1 to be resource, boolean given
in E:\Program Files\xampp\htdocs\DigitalLibrary\index.php on line 11
Warning: odbc_fetch_row() expects parameter 1 to be resource, null
given in E:\Program Files\xampp\htdocs\DigitalLibrary\index.php on
line 14
Warning: odbc_close() expects parameter 1 to be resource, boolean
given in E:\Program Files\xampp\htdocs\DigitalLibrary\index.php on
line 18
what am I doing wrong?
Now, You are attempting to use a 64 bit application with a 32 bit ODBC driver or vice versa. You should check that what are you using the PHP application 64 bit or 32 bit. After that you have to start the correct ODBC administrator. There are 2 types which are 32 bit and and 64 bit.
If you are using 64 bit ODBC administrator: It is in Adminstrative tools, control panel
If you are using 32 bit ODBC administrator: You should go into the path %windir\syswow64\odbcad32.exe.
When you have set the right ODBC administrator. You will also need to create the system DSN. If you cannot find the MS Access driver in the administrator you'll need to download one for that architecture.
EDIT : you can download the 32 bit from here
Hope this helps.
Related
I would like to acccess a mdb file in a windows machine (windows server 2003) from a linux machine (centos 6.10)
Im using mdbtools and unixodbc
so far what i did
odbc.ini
[mydatabase]
Driver = MDBTools
Description = Microsoft Access Try DB
Servername = localhost
Database =/mnt/example.mdb
UserName =Admin
Password =
port = 5432
odbcinst.ini
[MDBTools]
Description = MDBTools Driver
Driver64 = /usr/lib64/libmdbodbc.so.0
Setup64 = /usr/lib64/libmdbodbc.so.0
FileUsage = 1
UsageCount = 1
i check the connection using isql in terminal
isql -v mydatabase
and it returns connected
i can query the database successfully
by using query like this
select * from units
right now i want to connect the access database using php
test4.php
<?php
error_reporting(E_ALL); ini_set('display_errors', 1);
$myDB = odbc_connect("mydatabase","Admin","");
$query = "select * from units";
$result = odbc_exec($myDB, $query);
$report = odbc_fetch_row($result);
echo odbc_result($result, 1);
odbc_close($myDB);
?>
i received this error
Warning: odbc_connect(): SQL error: Failed to fetch error message, SQL state HY000 in SQLConnect in /var/www/test/test4.php on line 6
Warning: odbc_exec() expects parameter 1 to be resource, boolean given in /var/www/test/test4.php on line 11
Warning: odbc_fetch_row() expects parameter 1 to be resource, null given in /var/www/test/test4.php on line 12
Warning: odbc_result() expects parameter 1 to be resource, null given in /var/www/test/test4.php on line 13
Warning: odbc_close() expects parameter 1 to be resource, boolean given in /var/www/test/test4.php on line 15
im currently using
PHP Version 7.2.28
mySQL Version 5.7.29
Software:
PHP 7.3
MySQL Workbench 8.0
This is my PHP code:
try {
$link = mysqli_connect($DB_SERVER,$DB_USERNAME, $DB_PASSWORD,$DB_NAME,3306);
} catch (Exception $e){
throw new Exception("Can't execute command. ERROR: ".$e);
}
This is the error:
Warning: mysqli_connect(): PHP was built without openssl extension, can't send password encrypted in login.php on line 11
Warning: mysqli_connect(): (HY000/1045): Access denied for user 'XXXXX_admin'#'localhost' (using password: YES) in C:\inetpub\wwwroot\ on line 11
PHP Warning: mysqli_error() expects parameter 1 to be mysqli, boolean given in C:\inetpub\wwwroot\ on line 24
PHP Fatal error: Uncaught Exception: ERROR: Failed to connect to in C:\inetpub\wwwroot:24 Stack trace: #0 {main} thrown in C:\inetpub\wwwroot\ on line 24
Now the magic, when I login in to mysql workbench with the same user and password everything works fine. I have access to tables and other things. When I login in with my PHP application everything works also.
So my problem is everytime my server reboots I have to login in with MySQL Workbench, to get it working again. What am I missing?
You need to do verify few of the steps:
All necessary extensions are enabled like mysqli, openssl etc
Port is available
MySQLi is connecting at 3306 Port
Rest could be your username and password issue
Let us know once you will be verifying all above steps.
What you need to do is enable the openssl extension for php.
Open your php.ini file and search for the line:
;extension=php_openssl.dll
You need to uncomment it by removing the semicolon in front of it, leaving you with this:
extension=php_openssl.dll
Ive set up phpmyadmin and mySql on my windows server but when i try to connect via php i get this error :
Warning: mysqli::mysqli(): MySQL server has gone away in C:\xampp\htdocs\newservertest\dbscripts\sqlconnection.php on line 5
Warning: mysqli::mysqli(): Error while reading greeting packet. PID=15852 in C:\xampp\htdocs\newservertest\dbscripts\sqlconnection.php on line 5
Warning: mysqli::mysqli(): (HY000/2006): MySQL server has gone away in C:\xampp\htdocs\newservertest\dbscripts\sqlconnection.php on line 5
Catchable fatal error: Object of class mysqli could not be converted to string in C:\xampp\htdocs\newservertest\dbscripts\sqlconnection.php on line 14
I know by default mySql only allows local connections but following tutorials online they are all saying to uncomment a line in a "my.ini" file. All i have in my sql folder is a my-default.ini with the following :
How can i open this up to connections from an external php script?
Try adding this to your php.ini:
mysqli.reconnect = 1
I've installed unixodbc, freetds, php on Ubuntu 12.04.4 LTS (GNU/Linux 3.8.0-44-generic i686) and I get my query works fine with:
php /var/www/connect.php
but when access to localhost/connect.php I get:
Warning: odbc_connect(): SQL error: [unixODBC][Driver Manager]Data source name not found, and no default driver specified, SQL state IM002 in SQLConnect in /var/www/connect.php on line 8 Warning: odbc_exec() expects parameter 1 to be resource, boolean given in /var/www/connect.php on line 10 Warning: odbc_fetch_row() expects parameter 1 to be resource, null given in /var/www/connect.php on line 11 Warning: odbc_close() expects parameter 1 to be resource, boolean given in /var/www/connect.php on line 15
I think is about permissions.
As the title says, I'm having issues connecting to MSSQL from a PHP Script.
The setup:-
PHP is running on an Apache Linux Server.
Microsoft SQL Server 2008 is on an XP Machine.
I've got Remote Connections turned on in the MSSQL Server.
The database bggs does exist.
The database is running (I can see a green arrow).
I have no firewall on the XP Machine.
If you have any debugging questions, don't hesitate to ask.
I'm using the following test script.
<?php
include('adodb5/adodb.inc.php');
$db =& ADONewConnection('odbc_mssql');
$db->debug = true;
$dsn = "Driver={SQL Server};Server=ozmodiar;Database=bggs;";
$db->Connect($dsn,'user','password');
$rs = $db->Execute('select * from admin_users');
print "<pre>";
print_r($rs->GetRows());
print "</pre>";
?>
Getting the following result.
Warning: odbc_connect() [function.odbc-connect]: SQL error: [unixODBC][Driver Manager]Data source name not found, and no default driver specified, SQL state IM002 in SQLConnect in /mnt/filestore/vhost/bggs/hr_database/Dev/includes/adodb5/drivers/adodb-odbc.inc.php on line 60
(odbc_mssql): SET CONCAT_NULL_YIELDS_NULL OFF
Warning: odbc_exec(): supplied argument is not a valid ODBC-Link resource in /mnt/filestore/vhost/bggs/hr_database/Dev/includes/adodb5/drivers/adodb-odbc.inc.php on line 530
IM002: [unixODBC][Driver Manager]Data source name not found, and no default driver specified
ADOConnection._Execute(SET CONCAT_NULL_YIELDS_NULL OFF, false) % line 1017, file: adodb.inc.php
ADOConnection.Execute(SET CONCAT_NULL_YIELDS_NULL OFF) % line 62, file: adodb-odbc.inc.php
ADODB_odbc._connect(Driver={SQL Server};Server=192.168.70.130;Database=bggs;, sa, knd121, ) % line 524, file: adodb.inc.php
ADOConnection.Connect(Driver={SQL Server};Server=192.168.70.130;Database=bggs;, sa, knd121) % line 9, file: test_db.php
Driver={SQL Server};Server=192.168.70.130;Database=bggs;: [unixODBC][Driver Manager]Data source name not found, and no default driver specified
(odbc_mssql): select * from admin_users
Warning: odbc_exec(): supplied argument is not a valid ODBC-Link resource in /mnt/filestore/vhost/bggs/hr_database/Dev/includes/adodb5/drivers/adodb-odbc.inc.php on line 530
IM002: [unixODBC][Driver Manager]Data source name not found, and no default driver specified
ADOConnection._Execute(select * from admin_users, false) % line 1017, file: adodb.inc.php
ADOConnection.Execute(select * from admin_users) % line 10, file: test_db.php
Fatal error: Call to a member function GetRows() on a non-object in /mnt/filestore/vhost/bggs/hr_database/Dev/includes/test_db.php on line 12
Disclaimer: I've only queried data from Microsoft SQL Server 2000, but through PDO_DBLIB although I believe the pre-requisite of having freetds drivers for either ADODB or PDO_DBLIB a certainty.
Judging by the initial error message, you do not have necessary drivers installed. In order for your application to access the server, you will need to install freetds, which will install the necessary drivers and allow you to set up the the ODBC connections. Check out: http://www.linuxjournal.com/article/6636
It's important to note that you need to set the "tds version = x.x" in your freetds global config or specific OBDC connection (/etc/freetds/freetds.conf by default on debian) exactly right, or the protocol freedtds uses with your server will not be correct and the script will bomb.