How to connect to MSSQL DB with wamp64 - php

I'm using wamp64 as localhost for programming my php code.
But now I got asked to connect to a MSSQL DB.
I searched for answers on how to do it and found sqlsrv_connect() function.
When I call this function I get this error:
Uncaught Error: Call to undefined function sqlsrv_connect() in C:\wamp64\
I'm not familiar with this, so could someone help me with instructions on how to fix this?
I also read that I need to install!? (I don't understand how to do this)
My PHP version is 7.2.4
This is my code:
$serverName = "server, 1433"; //serverName\instanceName, portNumber
$connectionInfo = array( "Database"=>"DB", "UID"=>"USERNAME", "PWD"=>"PASSWORD");
$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));
}
I also downloaded some EXE files:
SQLSRV30.EXE, SQLSRV31.EXE, SQLSRV32.EXE, SQLSRV40.EXE
What do they mean? Which one should I have?
Please someone help me with this.
Edit: After trying the answer, I got this error:
PHP Startup: Unable to load dynamic library 'sqlsrv_72_nts'

I solved it ~ PHP Version 7.2.4
pdo_sqlsrv : 5.3.0+11108
1.download the proper version sqlsrv and sqlsrv pdo
2.put it into XAMPP\PHP\ext folder
3.after that write the line into php.ini > module setting
extension=php_pdo_sqlsrv_72_ts.dll
extension=php_sqlsrv_72_ts.dll
4.let's make a test for MSSQL still there is an error msg for recommending you to download ODBC Driver for SQL
Go to https://learn.microsoft.com/zh-tw/sql/connect/odbc/download-odbc-driver-for-sql-server?view=sql-server-2017 then download the proper file
(mine is Command Line Utilities 11 for SQL Server® X86)
6.fire the test.php again everything works!

I followed #SayedMohdAli's steps and Got this error:
PHP Startup: Unable to load dynamic library 'sqlsrv_72_nts'
I changed the name from 'sqlsrv_72_nts' to 'sqlsrv_72_ts' and It worked.
#SayedMohdAli thanks for helping me with this getting started. Solved it with your help.

Related

Why PHP don't see extension dll file?

I am little bit confused with strange behavior of PHP and need advice how to fix it.
I am tring to test pretty simple php script:
$conn = oci_connect($dbUser, $dbPassword, $dbServerName . "/" . $dbName);
if (!$conn) {
$e = oci_error();
trigger_error(htmlentities($e['message']), E_USER_ERROR);
}
Unfortunatly it raise me next ERROR:
[22-Oct-2018 19:22:23] PHP Warning: PHP Startup: Unable to load dynamic library 'F:\PHP_x64\ext\php_oci8_12c.dll' - The specified procedure could not be found.
in Unknown on line 0
[22-Oct-2018 19:22:23] PHP Fatal error: Call to undefined function oci_connect() in F:\TEST\complain\test.php on line 8
I have project which is located in IIS web server. Project use PHP Version 5.6.28.
Inside F:\PHP_x64\ext\ folder I have file php_oci8_12c.dll as you can see below:
Inside php.ini file I have uncommented line:
extension=php_oci8_12c.dll
Also in php.ini file I add:
extension_dir = "F:\PHP_x64\ext\"
[PHP_OCI8_12C]
extension=php_oci8_12c.dll
I restart IIS web server several times.
When I try to call php.exe from console it show me next error:
Finally I found solution which work for me.
The code which I use to connect PHP with Oracle:
$conn = oci_connect($dbUser, $dbPassword, "(DESCRIPTION=(ADDRESS_LIST =(ADDRESS=(PROTOCOL = TCP)(HOST=$dbServerName)(PORT = 1521)))(CONNECT_DATA=(SID=$dbSID)))", 'AL32UTF8') or die("Could not connect to ORACLE");
In my case I used SID, you can also use SERVICE_NAME.

PHP on IIS doesn't load sqlsrv extension

I'm trying to put together a page that can submit data from a form to an MSSQL database via PHP. I've got PHP installed successfully (phpinfo() runs fine even on a remote computer) but it will not load the sqlsrv extension. I know that I'm editing the right php.ini file because I have another extension added (wincache) and it is loading fine, confirmed with both phpinfo() and get_loaded_extensions().
The logs aren't telling me anything. PHP Manager on IIS is telling me that the sqlsrv extension files are enabled. I've restarted the website, the server, and the computer, and nothing's working.
I'm using PHP 7.1.1 on a Windows 7 Ultimate machine. It's running in x64 mode, Non-Thread Safe. I've made sure that the extension files I enabled are the x64, non-thread-safe ones. I don't know what else to check or what more info I need to provide.
EDIT: I ran php-cgi.exe as this page suggested, and it came out with the error mesages:
[31-Mar-2017 22:38:12 UTC] PHP Warning: PHP Startup: Unable to load dynamic library 'C:\Program Files\iis express\PHP\v7.1\ext\php_sqlsrv_7_nts_x64.dll' - The specified procedure could not be found.
in Unknown on line 0
[31-Mar-2017 22:38:17 UTC] PHP Warning: PHP Startup: Unable to load dynamic library 'C:\Program Files\iis express\PHP\v7.1\ext\php_pdo_sqlsrv_7_nts_x64.dll' - The specified procedure could not be found.
in Unknown on line 0
Does this mean that the problem is in the extension DLLs themselves? How would I even go about fixing that?
EDIT 2: ImClarky nailed the problem in that I was apparently using a way outdated version of the extension. I've got it working, but now my authentication is failing for reasons I don't understand:
function connect_sql() {
/* Specify server and connection attributes */
$serverName = "(local)";
/* Get UID and PWD from app-specific files */
$uid = file_get_contents("C:\AppData\uid.txt");
$pwd = file_get_contents("C:\AppData\pwd.txt");
$connectionInfo = array("UID" => $uid,
"PWD" => $pwd,
"Database" => "database_name");
/* Connect using SQL Server Auth */
$conn = sqlsrv_connect($serverName, $connectionInfo);
if ($conn === false)
{
echo "Unable to connect.</br>";
//die(print_r(sqlsrv_errors(), true));
die(print_errors());
}
else {
echo "Connection Successful";
}
/* Free connection resources */
sqlsrv_close($conn);
}
I've double checked that I am using the correct username and password, and I even went in and created an entirely new login using the exact UID and password I'm trying. I still get the following error message every time:
Unable to connect.
SQLSTATE: 28000
code: 18456
message: [Microsoft][ODBC Driver 13 for SQL Server][SQL Server]Login failed for user 'username'.
SQLSTATE: 28000
code: 18456
message: [Microsoft][ODBC Driver 13 for SQL Server][SQL Server]Login failed for user 'username'.
Make sure you add .dll file for extension folder used by php.ini
Also chose correct driver of SQLSRv matched your running PHP
SQLSRv sqlsrv from official microsoft page

How to setup a mongodb with php XAMPP?

I am a beginner to mongoDB, I want to learn mongoDB. So I read few tutorials and tried to configure mongoDB in my local machine, for that I have downloaded mondoDB's php_mongo.dll file and placed that on my local machine into the path "php/ext/php_mongo.dll" and after that I added this code "extension = php_mongo.dll" to php.ini file, after that I am trying run my XAMPP server but immediately it is showing the below error :
C:xampp\php\ext\php_mongo.dll is either not designed to run on Windows
or it contains an error. Try installing the program again using the
original installation media or contact your system administrator or
the software vendor for support.
after that somehow Apache server has been started And when I try to run the application on browser by URL http://localhost/mongoapp/index.php then it is throwing the following error:
Fatal error: Uncaught Error: Class 'MongoClient' not found in C:\xampp\htdocs\mongoapp\index.php:3 Stack trace: #0 {main} thrown in C:\xampp\htdocs\mongoapp\index.php on line 3
File: index.php
// connect to mongodb
$m = new MongoClient();
echo "Connection to database successfully";
// select a database
$db = $m->mydb;
echo "Database mydb selected";
My php version is "PHP Version 7.0.8". hope someone helps, Thanks in advance.
Check your phpinfo if mongodb is loaded.
Download mongodb from "https://www.mongodb.com/download-center?jmp=nav#community" and install.
Check your architecture in phpinfo match with mongodb.
­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­

SQLSERV in PHP7 not Working

I'm attempting to connect with a Sybase database with PHP7 on Windows Server 2012 R2 using the sqlsrv PDO driver. I've tried a number of connection strings - the following is the most direct connection string I've found that should be at least tossing me an error (an error from PHP) when accessing the page. However, when I access the page it it tells me the page is not working (as in the PHP is not working). PDO SQLSRV is active in my phpinfo(). The PHP Error I receive is:
[18-Nov-2016 22:32:18 UTC] PHP Warning: PHP Startup: Unable to load dynamic library 'C:\Program Files\PHP\v7.0\ext\php_wincache.dll' - The specified module could not be found.
in Unknown on line 0
And many times in the same log file I am receiving the following error:
[18-Nov-2016 22:31:23 UTC] PHP Warning: PHP Startup: Unable to load dynamic library 'C:\Program Files\PHP\v7.0\ext\php_mysql.dll' - The specified module could not be found.
in Unknown on line 0
This is all basically a straight-up install, so I'm surprised that it's tossing back errors about windows cache and mysql (when I'm not even attempting to access a mysql anything). Any leads or information would be great. Thank you for this forum!
<?php
try {
$server_name = "MY\SERVER,1234";
$database_name = "myDatabase";
$username = "myUsername";
$password = "myPassword";
$conn = new PDO("sqlsrv:server=$server_name;database=$database_name", "$username", "$password");
$conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
}
catch( PDOException $e ) {
die( "Error connecting to SQL Server" );
}
echo "Connected to SQL Server\n";
?>

sqlsrv driver showing up in phpmyinfo but connection still fails

I have successfully loaded the sqlsrv_pdo_54 driver that is right for me with ts and vc9 and php 5.4 which I am running, and it shows up in phpmyinfo. I have downloaded the windows native client, switched my port to 8080, and have wamp 32 bit running on my computer. However, whenever I run the following script
<?php
require_once 'trunk\global.inc.php';
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if($conn === false)
{
die(print_r(sqlsrv_errors(), true));
}
?>
I get the following error message:
Fatal error: Call to undefined function sqlsrv_connect() in C:\wamp\www\test.php on line 11
Apparently the function is still undefined. Why is this happening when the driver has been loaded? I tried restarting my server and it did not work.
sqlsrv_pdo doesn't use the standard sqlsrv_* functions. It uses PDO. Try changing your code to use PDO.
<?php
require_once 'trunk\global.inc.php';
$conn = new PDO("sqlsrv:Server=MY_SERVER;Database=MY_DATABASE", MY_USERNAME, MY_PASSWORD);
if($conn === false)
{
die('Unable to connect to DB');
}
?>

Categories