Python Forum
how do I successfully use PDO with my ODBC for MSSQL?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how do I successfully use PDO with my ODBC for MSSQL?
#1
  • Windows Server 2016 64 bit (Fully Updated)
  • WAMP/3.1.3 (Win32)
  • Apache/2.4.33 (Win32)
  • PHP/7.2.4 (Win32)

I am trying to connect to my database on a SQL Server but can't seem to get the connection working. I have tried several approaches, downloaded files, enabled extensions but none of them seem to work. I have established that the SQL Server has Named Pipes and that Remote Connections are allowed. I have also confirmed that he password and username work:

<?php

$driver='{ODBC Driver 17 for SQL Server}';
/* $driver='{ODBC Driver 13 for SQL Server}'; */
/* $driver='{ODBC Driver 11 for SQL Server}'; */
$dsn='phpodbc11';
$hostname='host01';
$database='data01';
$username='root';
$password='pass123';

$conn = new PDO("odbc:$dns")
$conn = new PDO("dblib:host=$hostname;dbname=$database", $username,$password);
$conn = new PDO("sqlsrv:Server=$hostname;Database=$database", $username, $password);
$conn = new PDO("odbc:Driver=$driver;Server=$hostname;Database=$database", $username, $password);

?>
[Image: AsZoR.jpg]

[Image: D2PiO.jpg]

php.ini extensions :
  • extension=pdo_odbc
  • extension=php_mysqli.dll
  • extension=php_ldap.dll
  • extension=php_pdo_sqlsrv_72_ts_x86.dll
  • extension=php_sqlsrv_72_ts_x86.dll

Errors I get for each of the aforementioned new PDO attempts

SQLSTATE[IM002] SQLConnect: 0 [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified

could not find driver

SQLSTATE[IMSSP]: This extension requires the Microsoft ODBC Driver for SQL Server to communicate with SQL Server. Access the following URL to download the ODBC Driver for SQL Server for x86: https://go.microsoft.com/fwlink/?LinkId=163712

SQLSTATE[IM002] SQLDriverConnect: 0 [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified
Reply
#2
This is Python forum, not PHP :-)
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
Smile oops, sorry !

Then python! Cool The above screenshots should still be valid. I actually started with python and then moved to PHP. The below basic code should just simply see if it can make a connection. So, THANK YOU as I was able to use python to get it working !!! And who knows, this might help in fixing the PHP too !

import pyodbc

con = ""

try:
    con = pyodbc.connect(
        r'DRIVER={ODBC Driver 11 for SQL Server};'
        r'SERVER=SERVER\SRVINSTANCE;'
        r'DATABASE=dbname;'
        r'UID=username;'
        r'PWD=password;'
        )
except pyodbc.Error, err:
    print err

if con:
       
    cur = con.cursor()
    
    con.close()
    
    print "yes! it works!"
Reply
#4
I personally use something like the following:

import pymysql

conn =  pymysql.connect(host=dbhost, port=3306,user=username,password=dbpassword,db='database',autocommit=True,local_infile=1)
cursor = conn.cursor()
cursor.execute(Query)
conn.close()
You should be able to do something similar with MSSQL using a DSN

conn = pyodbc.connect(r'DSN=mynewdsn;UID=user;PWD=password')
yea; here is a good example

https://docs.microsoft.com/en-us/sql/con...erver-2017
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  python connect to mssql wailoonho 7 1,399 Dec-07-2023, 02:06 AM
Last Post: wailoonho
Sad Can anyone help me successfully run this code? TheMouseAvenger 26 3,573 Sep-22-2023, 02:51 PM
Last Post: snippsat
  MSSQL query not working in Python kat35601 0 874 Apr-12-2022, 06:44 PM
Last Post: kat35601
  from MSSQL to excel kat35601 1 1,623 Apr-11-2022, 06:19 PM
Last Post: buran
  ssues with importing data from ODBC Slavek_d 1 1,377 Feb-01-2022, 09:57 AM
Last Post: ibreeden
  ODBC iPhone to PC KipCarter 0 1,478 Feb-19-2020, 11:53 AM
Last Post: KipCarter
  accessing Sybase (ASE and/or Anywhere) via ODBC... on Linux zatlas1 0 2,857 Jan-31-2019, 06:48 PM
Last Post: zatlas1
  [split] How to insert JSON into MSSQL gehrenfeld 3 3,255 Jan-27-2019, 06:20 PM
Last Post: micseydel
  How to insert JSON into MSSQL raysefo 2 4,107 Jan-27-2019, 06:17 PM
Last Post: micseydel
  Interface Arduino and MSSQL using Python MichaelDean 0 2,862 Nov-18-2018, 05:00 PM
Last Post: MichaelDean

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020