PHP - PDO SQLSRV - Connection string is not valid [87] - php

I´m trying to connect to a SQL Server 2008 R2 via PHP. I used the following code:
try {
$server = "servername.do.ma.in\\MSSQLSERVER";
$user = "username";
$pass = "password";
$db = "databasename";
$dbh= new PDO('sqlsrv:Server = ' . $server . '; Database = ' . $db, $user, $pass);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
echo 'Verbindung fehlgeschlagen: ' . $e->getMessage();
}
But when i run this code i get the following error:
SQLSTATE[08001]: [Microsoft][ODBC Driver 11 for SQL Server]SQL Server Network Interfaces: Connection string is not valid [87].
Does someone know this error and know what to do?
Thanks in advance.
Solution:
The Port was not defined. The following code worked for me.
I´ve added the Port 1433 to the $server Variable.
$server = "servername.do.ma.in, 1433";
$user = "username";
$pass = "password";
$db = "databasename";
$dbh = new PDO("sqlsrv:Server={$server};Database={$db};", $user, $pass);

This will work:
$dbh = new PDO("sqlsrv:Server={$server};Database={$db};", $user, $pass);
If it's still not valid, there's something wrong with your connection variables.
EDIT:
This looks similar to your problem:
http://laravel.io/forum/01-20-2015-sql-server-2012-db-connection-with-laravel-42

Related

PDO ODBC MYSQL : i try to connect and it just gives me a fatal error

Im trying to connect to odbc using the following php pdo code
$ligacao = new PDO("odbc:Driver={MYSQL ODBC 8.0 ANSI Driver };Server=localhost;Database=samsic; Uid=root;Pwd='';")
And it just gives me the following error:
Any thoughts on this?
As your database is local, as RiggsFolly said you don't need ODBC.
$db_host = "localhost";
$db_name = "samsic";
$db_user = "root";
$db_pass = "";
try{
$dbh = new PDO("mysql:host={$db_host};dbname={$db_name}",$db_user,$db_pass);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo 'success';
}
catch(PDOException $e){
die('ERROR: ' . $e->getMessage());
}

API with SLIM PHP in LAMP Server with MSSQL database

I have Lamp server in my Ubuntu. I worked on Api's using slim framework and with mysql database and its working fine. My problem is I cant connect my api to MSSQL.
I already install freetds in ubuntu
I also include this in freetds.conf
[myserver]
host = myhost
port = myport
here's my connection:
function getConnection() {
$dbhost="myserver.database.windows.net";
$dbuser="user";
$dbpass="mypass";
$dbname="myDB";
$dbh = new PDO("dblib:host=$dbhost;dbname=$dbname", $dbuser, $dbpass);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
return $dbh;
}
here's my api in slim:
$app = new Slim\App();
$app->get('/clients', 'getClients');
$app->run();
function getClients() {
$sql = "select * FROM mytable";
try {
$db = getConnection();
$stmt = $db->query($sql);
$clients = $stmt->fetchAll(PDO::FETCH_OBJ);
$db = null;
echo '{"client": ' . json_encode($clients) . '}';
} catch(PDOException $e) {
echo '{"error":{"text":'. $e->getMessage() .'}}';
}
}
I think you are missing something....
I think you need to let PDO know what port
http://php.net/manual/en/ref.pdo-dblib.php
$hostname = "myhost";
$port = 10060;
$dbname = "tempdb";
$username = "dbuser";
$pw = "password";
$dbh = new PDO ("dblib:host=$hostname:$port;dbname=$dbname","$username","$pw");

Can't connect mssql with PHP under mac Yosemite

I'm using PHP 5.5 under Mac Yosemite, the default php with this SO. i'm trying to connect to MSSQL DB server but it's imposible with a lot of alternatives.
I tried to install freetds and the command works but when i tried with PHP...its look he is trying to load but the connection close. My code on PHP is like this:
$server = 'XXX.XXX.XXX.XXX' ;
$user = "username";
$pass = "password";
$DB = "";
$link = mssql_connect($server, $user, $pass) ;
if(!$link){
die('Something goes wrong');
}
I look into php info and it's enabled:
php info
¿Someone knows what is the best alternative to connect to mssql db and works?
Use mssql_get_last_message() to find out what the error is, then, fix the problem.
$server = 'XXX.XXX.XXX.XXX' ;
$user = "username";
$pass = "password";
$DB = "";
$link = mssql_connect($server, $user, $pass) ;
echo mssql_get_last_message();
echo mssql_min_error_severity();
die();
Right now it's working with these lines:
try {
$hostname = 'XXX.XXX.XXX.XXX';
$port = 1433;
$dbname = "YOUR_DB";
$username = "YOUR_USERNAME";
$pw = "YOUR_PASS";
$dbh = new PDO ("dblib:host=$hostname:$port;dbname=$dbname","$username","$pw");
} catch (PDOException $e) {
echo "Failed to get DB handle: " . $e->getMessage() . "\n";
exit;
}
First you have to install PDO_DBLIB in your system.

Connect to SQL Server using PHP?

I am fairly new to using PHP. I downloaded XAMPP, and installed everything. PHP 5.5.27 is the version. I ran a test php program which was jsut echo "Hello World". It worked fine. I also was able to connect to MYSQL database using PHP.
$link = mysqli_connect("localhost", "u/n", "pass", "databasename";
Problem i am having and need help is with connecting to sql server. How do i do that? I saw an example online and tried it:
$serverName = "servername";
$connectionInfo = array("Database"="name", "UID"=>"U/N", "PWD"=>"pass";>
$conn = sqlsrv_connect($serverName, $connectionInfo);
But everytime i run this it tells me:
Call to undefined function sqlsrv_connect()
Can someone help me understand what is going on?
Consider using PHP's Data Objects (PDO) to connect to SQL Server (in fact you can use it to connect to MySQL or any other database).
Using the MSSQL sqlsrv API (various dlls must be set):
<?php
$server = 'servername';
$database = 'databasename';
$username = 'username';
$password = 'pass';
try {
$conn = new PDO("sqlsrv:Server=$server;Database=$database",
$user, $password);
}
catch(PDOException $e) {
echo $e->getMessage()."\n";
exit;
}
?>
Using the ODBC Driver or DSN API (requiring MSSQL ODBC Driver installed which usually ships with database or Windows in general):
<?php
$server = 'servername';
$database = 'databasename';
$username = 'username';
$password = 'pass';
try {
$dbh = new PDO("odbc:Driver={SQL Server};Server=$server;
database=$database",$username,$password);
}
catch(PDOException $e) {
echo $e->getMessage()."\n";
exit;
}
?>

Php connection with SQL Server 2012

I'm trying to make a connection to sql server 2012 with php, but always shows
Erro: SQLSTATE[IMSSP]: An invalid keyword 'Databases' was specified in the DSN string.
Can someone help me with this?
here is the code that i use to make the connection
<?php
session_start();
$servername = 'SERVERNAME';
$username = 'sa';
$password = '12345678';
$dbname = 'DBNAME';
//connection
try {
$conn = new PDO("sqlsrv:server=$servername;database=$dbname", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$conn->setAttribute(PDO::SQLSRV_ATTR_DIRECT_QUERY , true);
} catch(PDOException $e) {
echo "Erro: " . $e->getMessage();
}
$conn=null;
?>
I haven't used SQL server 2012 but try changing
$conn = new PDO("sqlsrv:server=$servername;database=$dbname", $username, $password);
To
$conn = new PDO("mssql:host=$servername;dbname=$dbname", $username, $password);

Categories