Connect to SQL Server using PHP? - 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;
}
?>

Related

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.

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.

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

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

how to connect php with mysql

I am very new to php. I am trying to make a sign up page with php. So simple but I can't. I've searched across google how to connect it. They said below.
<?PHP
$user_name = "root";
$password = "";
$database = "user";
$server = "127.0.0.1";
mysql_connect($server, $user_name, $password);
print "Connection to the Server opened";
?>
I've successfully created my user database with full set of data in phpmyadmin. And running Apache and Mysql in control panel. I set the password in phpmyadmin and I changed it in $password="mypassword";. But there is no print on my web page. I think the above code is correct and I am having problems before this state. Such as the location of my database, I don't where to put it or just created on myadmin is fine. Thank you for reading my problem and kindly advice me for the beginner course.
<?php
$user_name = "root";
$password = "";
$database = "user";
$server = "127.0.0.1";
// Create connection
$con=mysqli_connect($server, $user_name, $password, $database );
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
Try mysqli instead of mysql and here is why
no no . . sorry . My html path . . on desktop
PHP, in this context, is a server side programming language. You must request the script from a server that will pass it through a PHP interpreter before sending it to the browser.
Type http://localhost/etc/etc into the browser's address bar. Don't open the PHP program directly in your browser (e.g. by double clicking the file in Windows Explorer).
If you are going to use procedural style function calls with the mysqli interface:
$con = mysqli_connect('localhost','myuser','mypassword','mydb')
or die("Error " . mysqli_error($con));
If you are going to use object oriented style with mysqli interface:
$mysqli = new mysqli('localhost', 'myuser', 'mypassword', 'mydb');
Reference: http://php.net/manual/en/mysqli.construct.php
If you are going to use PDO interface
$dbh = new PDO('mysql:host=localhost;dbname=mydb', $user, $pass);
Reference: http://php.net/manual/en/pdo.connections.php

Connecting to a remote mysql server

How would I connect to the demo phpmyadmin server in php? My code looks like this.
<?php
$host = 'http://demo.phpmyadmin.net/STABLE/';
$dbname = 'shubham';
$user = 'root';
$pass = '';
// Attempt to connect to database.
try {
$DBH = new PDO("mysql:host={$host};dbname={$dbname}", $user, $pass);
} catch(PDOException $e) {
echo $e->getMessage();
}
?>
but I get this as my error
QLSTATE[HY000] [2005] Unknown MySQL server host 'http://www.demo.phpmyadmin.net/STABLE/' (1)
You seem to be confusing two things:
the demo phpMyAdmin front-end that is backed by a db server and db/schema
the db server and schema itself
PDO needs the latter, the db server itself.
Inspecting the front-end code of the demo, I don't see anything in there that would give us the actual connection details for the db server. And that's as I would expect: I find it hard to believe that the makers/maintainers of the phpMyAdmin demo would make their actual db server available for public remote connections.
change your hostname from
$host = 'http://demo.phpmyadmin.net/STABLE/';
to your original remote hostname like eg $host = 'ukld.db.5510597.hostedresource.com';
MySQL does not work on HTTP
<?php
$host = 'demo.phpmyadmin.net';
// High chances that this is NOT your mysql hostname.
// It will not even by like /STABLE/ as you mentioned it.
$dbname = 'shubham';
$user = 'root';
$pass = '';
// Attempt to connect to database.
try {
$DBH = new PDO("mysql:host={$host};dbname={$dbname}", $user, $pass);
} catch(PDOException $e) {
echo $e->getMessage();
}
?>

Categories