Error connecting to MSSQL using PHP - php

I am receiving an error as below:
PHP Fatal error: Uncaught exception 'PDOException' with message
'SQLSTATE[08001]: [Microsoft][ODBC Driver 11 for SQL Server]SQL Server
Network Interfaces: Connection string is not valid [87]. '
My codes are as follow:
$this->link = new PDO(
"sqlsrv:server=$this->serverName:$this->port;Database=$this->db",
"$this->uid",
"$this->pwd"
);
I wish someone can enlighten me on what is causing this error. Before that I was using dblib instead of sqlsrv.
"dblib:host=$this->serverName:$this->port;dbname=$this->db"
I am using XAMMP with Apache 2.4.12 and PHP 5.5.24 (they are all x86). I have php_pdo_sqlsrv_55_ts.dll and php_sqlsrv_55_ts.dll. I am also using Microsoft ODBC Driver 11 for SQL Server-x64.

Change it to:
$this->link = new PDO(
"sqlsrv:Server={$this->serverName},{$this->port};Database={$this->db};",
$this->uid,
$this->pwd
);
The default SQL Server port is 1433. Note the curly brackets, they allow for class variables.

Related

PHP ODBC driver for mac os

i need to connect to ms sql server using odbc_connect.
this is line for connection in my get_conn function
self::$conn = odbc_connect($data['server'], $data['username'], $data['password']);
this is how i execute queries in my app
$rs = odbc_exec(self::get_conn(), $query);
On my windows 10 machine this worked just fine, after i have installed odbc driver for php, but i cant find the odbc driver for mac os.
Error Message
Fatal error: Uncaught Error: Call to undefined function odbc_exec()
Have someone have used this odbc functions on mac os, and how did u configure it?

PHP ODBC - connect to local .mdb database

I've installed the php5-odbc library.
Trying to connect to a .mdb file but keep getting error Data source name not found, and no default driver specified.
Code:
$dbName = "../../../var/Import/PartsPlaceDB.mdb";
if (!file_exists($dbName)) {
die("Could not find database file.");
}
$db = new PDO("odbc:DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=$dbName; Uid=; Pwd=;");
Outputs:
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[IM002] SQLDriverConnect: 0 [unixODBC][Driver Manager]Data source name not found, and no default driver specified' in [...]
PDO Drivers:
foreach(PDO::getAvailableDrivers() as $driver)
{
echo $driver.'<br />';
}
Output:
mysql
odbc
sqlite
The problem is recurrent with 64-bit version. It looks like your code only work on Windows 32-bit.
To solve the problem, you might install this tool: Microsoft Access Database Engine 2010 Redistributable. I had the same problem for months but this tool solved mine.
I guess you're doing the location wrong?
Instead of this
$dbName = "../../../var/Import/PartsPlaceDB.mdb";
Why not do it like this
$dbName = "..\..\..\var\Import\PartsPlaceDB.mdb";

connecting php 5.2.4 with MSSQL server 2008 using pdo

I use php 5.2.4, and I am trying to connect with the MSSQL database, without success.
My php info show this:
mssql
MSSQL Support enabled
Active Persistent Links 0
Active Links 0
Library version 7.0
pdo_mssql
PDO Driver for MSSQL DB-lib enabled
Flavour MSSQL_70
My PHP code is: as follow:
$db = new PDO("sqlsrv:Server= servername\sqlservername;Database=mydb", "myuser", "mypass");
var_dump($db);
The error message displays:
Fatal error: Uncaught exception 'PDOException' with message 'could not find driver'
Anyone have idea what is wrong?

php_connect to odbc source produces TNS:could not resolve service name error after Xampp reinstall

I just updated my Windows Xampp install to get the latest PHP version and after the reinstall my web app is giving me the following error when trying to connect to an Oracle data source via the Microsoft ODBC driver for Oracle.
Warning: odbc_connect(): SQL error: [Microsoft][ODBC driver for
Oracle][Oracle]ORA-12154: TNS:could not resolve service name, SQL
state 08001 in SQLConnect in C:\xampp\htdocs\webapp\connections\db.php
on line 34
The code I'm using for this is;
$dsn = 'dsnname';
$user = 'username';
$pass = 'password';
$conn = odbc_connect($dsn, $user, $pass, SQL_CUR_USE_ODBC);
This was working fine before I did the Xampp update and now suddenly it's not working. I think I had this problem once before but I'm convinced it was solved by adding the SQL_CUR_USE_ODBC cursor.
Anyone got any ideas on what could be wrong?

Trying to connect PDO on a local server

I am trying to connecting PDO on my local server. I'm using Windows 7 Professional 64-bit and have AppServ installed. In a php document I use the following code for connection:
<?php
$dbh = new PDO('mysql:host=localhost;dbname=test', 'root', 'password');
?>
But the code turns out to be an error: Fatal error: Uncaught exception 'PDOException' with message 'could not find driver' in C:\AppServ\www\a.php:2
I checked phpinfo() and it shows that sqlite and sqlite2 are the only two databases installed in PDO. So how can I install MySQL PDO?
Thank you all. I just removed the semicolon in the php.ini file for MySQL and it works! The default location of php.ini file is in C:/Windows/.

Categories