Problems with xampp - php

I can not recognize the SQL Server command in XAMPP 1.8.1 and PHP 5.4.7, this code had already used it and it worked before but now it doesn't.
I have already tried to put the php_mssql.dll dll and still don't recognize the command:
$con = mssql_connect('187.164.1.2/base','pag','123') or die('Could not connect to the server!');
mssql_select_db('aguacom') or die('Could not select a database.');
I expected it to connect to the other server's database.

You are trying to connect to MS SQL Server using MSSQL PHP extension (mssql_ functions), but this extension is not available anymore on Windows with PHP 5.3 and removed in PHP 7.0.0.
What you can do is to install PHP Driver for SQL Server (sqlsrv_ functions). You need to download:
The appropriate version of this driver - for PHP 5.4 you need version 3.2 (32-bit or 64-bit depending on the PHP version)
The appropriate ODBC driver.
Note, that MSSQL PHP extension (php_mssql.dll) and PHP Driver for SQL Server (php_sqlsrv_54_ts.dll) are two different PHP extensions.
Example using mssql_ functions:
<?php
$server = "187.164.1.2/base";
$username = "pag";
$password = "123";
$database = "aguacom";
$conn = mssql_connect($server, $username, $password);
if ($conn === false) {
echo "Unable to connect. ".mssql_get_last_message()."</br>";
exit;
} else {
echo "Connected.</br>";
}
mssql_select_db($database, $conn);
// ...
mssql_close($conn);
?>
Example using sqlsrv_ functions:
<?php
$server = "187.164.1.2/base";
$username = "pag";
$password = "123";
$database = "aguacom";
$connectionInfo = array(
"UID" => $username,
"PWD" => $password,
"Database" => $database
);
$conn = sqlsrv_connect($serverName, $connectionInfo);
if ($conn === false) {
echo "Unable to connect. ".print_r(sqlsrv_errors(), true)."</br>";
exit;
} else {
echo "Connected.</br>";
}
// ...
sqlsrv_close($conn);
?>

you should enable mssql in xampp
things you need [download these files]: https://www.microsoft.com/en-us/download/details.aspx?id=20098
Add downloaded files in php.ini like this way
extension=php_sqlsrv_56_ts.dll
extension=php_pdo_sqlsrv_56_ts.dll
restart apache and then check
<?php
echo "<pre>";
print_r(PDO::getAvailableDrivers()); ?>
credits goes to rray

Related

cant connect to mssql server or sqlsrv not showing up on phpinfo

I have problem to connect to SQL server. I did all the steps from here and I already changed the php.ini configuration file:
;On windows:
extension_dir = "D:\xampp\php\ext"
But I still can't connect, my PHP Version is 7.2.11. I tried with mssql_connect() and sqlsrv_connect():
This is my attempt:
<?php
$servername = "1111";
$username = "user";
$password = "123";
$dbname = "DEV";
$connection = mssql_connect($servername, $username, $password);
if (!$connection) { die('Not connected : ' . mssql_get_last_message());}
$db_selected = mssql_select_db($dbname, $connection);
if (!$db_selected) {
die ('Can\'t use db : ' . mssql_get_last_message());
} else{
echo "success";}
?>
MSSQL extension for PHP (mssql_ functions) and PHP Driver for SQL Server (sqlsrv_ functions) are two different extensions for PHP.
MSSQL extension is not available anymore on Windows with PHP 5.3 or later, so you need to install PHP Driver for SQL Server. You need to download appropriate version of this driver. For PHP 7.2 - version 5.2 or 5.3 (32-bit or 64-bit also depends on PHP version).
Also download and install an appropriate ODBC driver.
Check the configuration with <?php phpinfo();?>. There should be a section with name pdo_sqlsrv (if you use PDO) and/or sqlsrv (without PDO).
Example:
<?php
$server = '1111';
$database = 'DEV';
$uid = 'user';
$pwd = '123';
# SQL Server authentication
#$cinfo = array(
# "Database" => $database,
# "UID" => $uid,
# "PWD" => $pwd
#);
# Windows authentication
$cinfo = array(
"Database" => $database
);
$conn = sqlsrv_connect($server, $cinfo);
if ($conn === false) {
echo "Error (sqlsrv_connect): ".print_r(sqlsrv_errors(), true);
exit;
} else {
echo "success";
}
sqlsrv_close($conn);
?>

php mssql linux

I have a php file with a mssql connection, which works on windows.
Now I would like to run my php on linux.
I installed unixodbc and msodbcsql.
The server is running RedhatEnterprise 6 and php 5.4
I cant find any description how to connect to a mssql database with linux.
What else do I have to install and configure?
You can certainly connect directly to Microsoft SQL Server database from a Linux server, using PHP's PDO system and the dblib driver.
First, install the EPEL repository and then install the php-mssql package. Then use the following code to set up your connection:
$dbhost = "your.db.server";
$dbname = "yourdbname";
$dbuser = "yourdbuser";
$dbpass = "yourdbpassword";
$db = new PDO("dblib:$dbhost;dbname=$dbname", $dbuser, $dbpass);
foreach ($db->query("select * from mytable") as $row) {
echo "{$row['field1]'}<br/>";
}
Additional StackOverflow resources on this topic include:
Connecting to mssql using pdo through php and linux
pdo dblib on centos 6.x
I faced the same challenge very recently, and after a lot of head scratching ultimately, I decided to go about things a little differently, as follows.
I set up a Windows machine (it could be a virtual machine) running PHP and Apache to handle the MSSQL requests from the Linux box.
The PHP on Linux uses a CURL command to send a request to Windows, and the response contains the data (compressed).
A little off-beat perhaps, but working well.
You need to have the modules sqlsrv and pdo_sqlsrv active.
sudo apt-get install php-fpm pcp-cli php-dev php-pear
sudo pecl install sqlsrv pdo_sqlsrv
The correct location (on Linux) to put these two lines is in /etc/php/<version>/mods-available/pdo.ini
there, add
extension=sqlsrv.so
extension=pdo_sqlsrv.so
Then restart/reload php-fpm or the computer.
Then run ./test.php (where port 2017 is the SQL-server-port)
#!/usr/bin/php
<?php
$serverName = "localhost,2017";
$connectionOptions = array(
"database" => "MY_DB_NAME",
"uid" => "sa",
"pwd" => "TOP_SECRET"
);
// Establishes the connection
$conn = sqlsrv_connect($serverName, $connectionOptions);
if ($conn === false) {
die(formatErrors(sqlsrv_errors()));
}
// Select Query
$tsql = "SELECT ##Version AS SQL_VERSION";
// Executes the query
$stmt = sqlsrv_query($conn, $tsql);
// Error handling
if ($stmt === false) {
die(formatErrors(sqlsrv_errors()));
}
?>
<h1> Results : </h1>
<?php
while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
echo $row['SQL_VERSION'] . PHP_EOL;
}
sqlsrv_free_stmt($stmt);
sqlsrv_close($conn);
function formatErrors($errors)
{
// Display errors
echo "Error information: <br/>";
foreach ($errors as $error) {
echo "SQLSTATE: ". $error['SQLSTATE'] . "<br/>";
echo "Code: ". $error['code'] . "<br/>";
echo "Message: ". $error['message'] . "<br/>";
}
}
?>
And to query the db with SQL-server pdo:
$servername = "";
$username = "";
$password = "";
$database = "";
$port = "1433";
try
{
$dbh = new PDO("sqlsrv:server=$servername,$port;Database=$database;ConnectionPooling=0", $username, $password,
array(
PDO::ATTR_PERSISTENT => true,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
)
);
}
catch (PDOException $e)
{
echo ("Error connecting to SQL Server: " . $e->getMessage());
}
$stmt = $dbh->prepare("SELECT name FROM master.sys.databases WHERE name = db_name()");
$stmt->execute();
while ($row = $stmt->fetch()) {
print_r($row);
}
unset($dbh); unset($stmt);

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

PDO or MSSQL_connect - in PHP 5.3

I am working on a website that runs by MYSQL and Linux Php 5.3 - and i need to work with this as well as a remote MSSQL database.
I read that PDO this is the way to connect to MSSQL.
It though seems there are both a PDO and a more familiar mssql_connect solution.
I have little to no experience with either PDO or mssql_connect.
On the PHP documentation i find:
Mssql_connect - The familiar expression:
<?php
// Create a link to MSSQL
$link = mssql_connect('KALLESPC\SQLEXPRESS', 'sa', 'phpfi');
// Select the database 'php'
mssql_select_db('php', $link);
?>
PDO - Which i haven't tried before - which needs a driver !(?) :
<?php
/* Connect to an ODBC database using driver invocation */
$dsn = 'mysql:dbname=testdb;host=127.0.0.1';
$user = 'dbuser';
$password = 'dbpass';
try {
$dbh = new PDO($dsn, $user, $password);
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
?>
So what to choose and why ?
Although I have not tried it yet. So I can't say for sure if it works or not.
PHP Manual says use pdo::dblib http://php.net/manual/en/ref.pdo-dblib.php
Microsoft does have it's own set of drivers but you have to be on a windows machine to use them. http://www.microsoft.com/en-us/download/details.aspx?id=20098
MSSQL connection with PDO:
$db_handle = new PDO("sqlsrv:server=$server; Database=$database", $user, $pass);
MySQL connection with PDO:
$db_handle = new PDO("mysql:host=$server;dbname=$database", $user, $pass);
I don't see what your confusion is?

How to establish a connection?

I am new to DB2.
$database = 'test';
$user = 'user1';
$password = 'pswd1';
$hostname = '10.250.10.10';
$port = 556;
$conn_string = "DRIVER={IBM DB2 ODBC DRIVER};DATABASE=$database;" .
"HOSTNAME=$hostname;PORT=$port;PROTOCOL=TCPIP;UID=$user;PWD=$password;";
$conn = db2_connect($conn_string, '', '');
if ($conn) {
echo "Connection succeeded.";
db2_close($conn);
}
else {
print($conn);
echo "Connection failed.";
die(db2_conn_errormsg());
}
all the values are correct. But connection have been failed. Plz advise me. and How to check in the PHPINFO(). whether the DB2 have been installed successfully or not.
I got the following error
Connection failed.[unixODBC][Driver Manager]Data source name not found, and no default driver specified SQLCODE=0
I'm not a PHP maven, but if you're using the ODBC driver, you probably need to catalogue the database in the ODBC driver manager. On Windows, search for ODBC in "Search Programs and Files". I'm not sure the equivalent on Linux

Categories