PHP PDO dblib not working - php

I am trying to connect to MS SQL database which is hosted on a different set of servers. I can connect the old way.
$myServer = "server name";
$myUser = "username";
$myPass = "pword";
$myDB = "dbname";
if (is_callable('mssql_connect')) {
$link = mssql_connect($myServer, $myUser, $myPass);
if (!$link) {
die('connection failed');
}
} else {
echo 'mssql_connect() is not supported on this environment';
}
//connection to the database
$dbhandle = mssql_connect($myServer, $myUser, $myPass);
//select a database to work with
$selected = mssql_select_db($myDB, $dbhandle)
or die("Couldn't open database $myDB");
That works just fine to connect to the ms sql server. When I try with PDO dblib I get the following error
"SQLSTATE[01002] Adaptive Server connection failed (severity 9)"
Here is the code I am using for that and I have checked and pdo is installed with dblib as an option to use. Credentials are all exactly the same.
$myServer = "server name";
$myUser = "username";
$myPass = "pword";
$myDB = "dbname";
try {
# MS SQL Server and Sybase with PDO_DBLIB
$DBH = new PDO("dblib:host=$myServer;dbname=$myDB, $myUser, $myPass");
}
catch(PDOException $e) {
echo $e->getMessage();
}
Any help would be appreciated as I am planning to create a new application in php to connect to this MS SQL db but in the future plan to migrate db over to Mysql once all of the old classic asp's are rebuilt into php.

The PDO connection syntax is supposed to be like this,
$DBH = new PDO("dblib:host=$myServer;dbname=$myDB", $myUser, $myPass); //wrongly placed quotes
Manual

Related

Database connection Unknown error in PHP with MySQL server workbench not in phpmyadmin

I`m working on a java swing app. so that i have created a database called c_app in MySQL server workbench. what i need is to fetch data into a HTML page using PHP.
$username = "root";
$password = "";
$hostname = "localhost:3307";
$db = "c_app";
//connection to the database
$dbhandle = mysqli_connect($hostname, $username, $password, $db)
or die("Unable to connect to MySQL");
This code segment gives me Warning: mysqli_connect(): (HY000/1049): Unknown database 'c_app' in C:\xampp\htdocs\C_AppWeb\linkpage.php on line 7
Unable to connect to MySQL
What is the problem here? do i need phpmyadmin database rather than MySQl database? can anyone help me?
First one, both phpmyadmin and workbench are not database. They are tools to work with database.
I see you use port 3307 in your code, so let you try:
$username = "root";
$password = "";
$hostname = "localhost";
$port = 3307;
$db = "c_app";
//connection to the database
$dbhandle = mysqli_connect($hostname, $username, $password, $db, $port)
or die("Unable to connect to MySQL");

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;
}
?>

Connecting php to MS SQL server 2008

I need a connection script of php and MS SQL:
in trying the following but it doesn't work
$server = 'MVEKELDG156\SQLEXPRESS'; $username = 'EDUC.PWV.GOV.ZA\Mveke.L';
$password = 'password#'; $database = 'SAMS_EMIS_Warehouses';
if(!mysql_connect($server, $username, $password)) { exit('Error: could not
establish database connection'); } if(!mysql_select_db($database)) {
exit('Error: could not select the database'); }
The MSSQL extension is enabled by adding extension=php_mssql.dll to php.ini.
To get these functions to work, you have to compile PHP with --with-mssql[=DIR] , where DIR is the FreeTDS install prefix. And FreeTDS should be compiled using --enable-msdblib .
More References :
Installation
How to use PHP to connect to sql server
Connecting to an SQL Server Database with PHP
Check this
$myServer = "localhost";
$myUser = "your_name";
$myPass = "your_password";
$myDB = "SAMS_EMIS_Warehouses";
//connection to the database
$dbhandle = mssql_connect($myServer, $myUser, $myPass)
or die("Couldn't connect to SQL Server on $myServer");
//select a database to work with
$selected = mssql_select_db($myDB, $dbhandle)
or die("Couldn't open database $myDB
Source How to connect to MS SQL Server database

MSSQL using PDO to connect

I am trying to connect to a mssql server on my mac through pdo_dblib. I have the freetds.conf updated to the host I want to connect. My phpinfo tells me that I have all the driver hooked up and good to go. Below is the code that I wrote to test if I can connect to the server.
<?php
$servername = "IP";
$port = "port";
$username = "username";
$password = "password";
$myDB = "database";
try {
$conn = new PDO("dblib:dbname=$myDB;host=$servername:$port", "$username", "$password");
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Connected successfully";
}
catch(PDOException $e)
{
echo "Connection failed: " . $e->getMessage();
}
?>
However I get an error:
Connection failed: SQLSTATE[] (null) (severity 0)
I tried using SQL Developer to connect to the mssql server and it worked. I have been trying to solve this problem for a whole day. What might be the problem? Thanks!
Remove the quotes from the variables in the connection string -
$conn = new PDO("dblib:dbname=$myDB;host=$servername:$port", $username, $password);
I found out the answer!
You have to uncomment TDS version protocol in /usr/local/Cellar/freetds/0.91/etc/freetds.conf.
Found the solution in Why won't my server connect to a remote MSSQL server using PHP mssql_connect?
but why do I have to do this...no idea...wish some one could give some explanation.

Connect to Microsoft SQL Server PHP on Mac

I have a Macbook with Yosemite. I download an app from apple store to try to run PHP code. I want to connect to a Microsoft SQL Server and this is the code:
$serverName = "astidbs";
$connectionInfo = array( "Database"=>"generale1", "UID"=>"xxxx", "PWD"=>"xxxx");
$conn = sqlsrv_connect($serverName, $connectionInfo);
if( $conn ) {
echo "Connection established.<br />";
}else{
echo "Connection could not be established.<br />";
die( print_r( sqlsrv_errors(), true));
}
This is the error:
Fatal error: Call to undefined function sqlsrv_connect() in /private/var/folders/t5/zjgg2nv149x48bqqdhvnllcw0000gn/T/com.kukosk.PHP-Code-Tester/PHP_Code_Tester_tempfile.php on line 5
I can't understand if it was an app problem so I tried the code on an online tester but it doesn't work either. The error is the same but without the path. It seems that there isn't the lib for the specified call so I have searched on Google on how to install that but I wasn't able to solve the problem. Anyone have a solution?
Try the following:
<?php
$myServer = "astidbs";
$myUser = "your_name";
$myPass = "your_password";
$myDB = "examples";
//connection to the database
$dbhandle = mssql_connect($myServer, $myUser, $myPass)
or die("Couldn't connect to SQL Server on $myServer");
//select a database to work with
$selected = mssql_select_db($myDB, $dbhandle)
or die("Couldn't open database $myDB");
//close the connection
mssql_close($dbhandle);
?>

Categories