I'm trying to connect a php script to a Microsoft SQL Server, Additionally I'm doing some testing on my own to learn more about php and servers.
if(function_exists("sqlsrv_connect"))
{
echo "exists<br/>";
}else
{
die("does not exist<br/>");
}
$serverName = "myTestServer";
$connectionInfo = array( "Database"=>"test_name", "UID"=>"testUser", "PWD"=>"testPwd");
$conn = sqlsrv_connect($serverName, $connectionInfo);
if($conn)
{
echo "sucessful";
}
else
{
echo "failed ";
}
The output is:
exists
failed
Therefore I can see that the .dll was installed as intended. Am I missing something else? or could this be a problem in the server properties?
Whenever a failure occurs with a SQLSRV PHP function, call sqlsrv_errors to find out the root cause. In your case, it seems you don't have the correct SQL Server ODBC drivers installed on the PHP server. Install the drivers as recommended by your error message, and things should improve.
Related
Actually I have try to connect SQL server by using sqlsrv_connect and using the DSN (Data Source Name) without Apache service then the both are perfectly working.
The problem is when I turn on the apache service then php cannot connect to the sql server using odbc DSN (Working with sqlsrcv_connect).
The condition is I need to turn on the apache with a application running using sql server which using DSN. I working with crystal report thats why really need this method. I have tried using system DSN instead user dsn, its also not working.
!
I wrote code below to test my scenario
// Connect to the data source
$conn=odbc_connect('DSNNAME','DBUSER','DBPASS');
if ($conn){
echo "Connection established DSN";
}
else {
echo "Connection using DSN Failed:" . odbc_errormsg();
}
// Connect through server name
$serverName = "WEBSERVER\SQLEXPRESS"; //serverName\instanceName, portNumber (default is 1433)
$connectionInfo = array( "Database"=>"DBNAME", "UID"=>"DBUSER", "PWD"=>"DBPASS");
$conn2 = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn2 ) {
echo "Connection established using server name";
}else{
echo "Connection could not be established by using server name";
die( print_r( sqlsrv_errors(), true));
}
The Result when Apache service is on.
Make sure the ODBC DSN matches the Crystal runtime in terms of 32-bit or 64-bit.
I have searched through many posts trying to find an answer but get nowhere.
I am trying to establish a connection to and Sql Server DB through my PHP web application using WAMP.
What I have tried:
I downloaded the sql drivers for PHP 7 and 7.1 and tried them with the corresponding PHP versions
I made sure to restart all services after updating the php.ini file.
I haveinstalled the SQLSRV40.EXE and updated the php.ini with:
extension=php_pdo_sqlsrv_7_ts_x64.dll
extension=php_pdo_sqlsrv_7_nts_x64.dll
I did not though that even though these are added in the php.ini they were not in the php> php extentions list - not sure why
This is my code below allow with the error
<?php
$serverName="DESKTOP-0KNJ0KP";
$connectionInfo=array("Database"=>"SPMS_db",);
$conn=sqlsrv_connect($serverName,$connectionInfo);
if ($conn) {
echo "Connected.<br />";
} else {
echo "Connection failed.<br />";
die( print_r( sqlsrv_errors(), true));
}
?>
I have added context fro PHPinfo()
You have installed PDO_sqlsrv part of PHP Driver for SQL Server, but your code uses sqlsrv functions. You have two options:
install php_sqlsrv_ extensions to make these functions work or
rewrite your code to use PDO version of the driver
PHP code using PDO version of PHP Driver for SQL Server:
<?php
# Connection
$server = "DESKTOP-0KNJ0KP";
$database = "SPMS_db";
try {
$conn = new PDO( "sqlsrv:Server=$server;Database=$database", NULL, NULL);
$conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
} catch( PDOException $e ) {
die( "Error connecting to SQL Server. ".$e->getMessage() );
}
# End
$conn = null;
?>
Connecting through PDO library (which is already installed in your server) is easier.
See PDO Book from PHP.NET: https://www.php.net/manual/en/ref.pdo-dblib.php
I am running Windows 7, I use XAMPP, I created my database from command line using sql create local ... When opening SQL SERVER Management Studio My server name is (LocalDB)\LocalDB ... Inclduing The brackets... But when I use the server name to connect to the database I get an error message... My connection code is below... I have added my extensions and enabled them in my php.ini file... I need help as to how can I determine which server name can I use as I assume that could be the problem... I have also tried using LocalDB as it is as just the server name but no luck... I use Windows Authentication Method on my SQL Server Management Studio...
<?php
if($connectInfo = array("UID"=>"userName", "PWD"=>"password",'Database'=>'localSample')){
echo "Array Set Up" . "<br/>";
}else{
echo "Could Not Set Array";
die();
}
$serverName = "LocalDB";
if($conn = sqlsrv_connect($serverName, $connectInfo)){
echo "Good Connection Good... " . "<br/>";
}else{
echo "Connection Not Good";
die();
}
?>
I have found a solution... Seems I also needed to download an ODBC Driver for SQL Server.... I found it on the link below... All is good now
https://www.microsoft.com/en-us/download/details.aspx?id=36434
I am trying to use Microsoft server in a PHP program. I downloaded the Microsift drivers for PHP for SQL Server, made sure that Microsoft SQL Server 2012 Client was installed, made sure the extension_dir value was correct in my PHP.ini file, and added extension=php_sqlsrv_53_ts.dll in the dyanmic extension section of my PHP.ini file. Yet i still get the error 'Fatal error: Call to undefined function sqlsrv_connect()'. I am running an apache server on windows 7 and downloaded the 3.0 version of the driver.
So what am I doing wrong? What else should I check for? If I am running under an Apache server, I don't need to do anything to IIS, do I? Below is my PHP code.
<?php
// phpinfo(INFO_MODULES);
require_once 'serverlogin2.php';
/* Specify the server and connection string attributes. */
//$connectionInfo = array ("Database"=>$databaseName, "UID"=>$uid, "PWD"=>$pwd);
$connectionInfo = array ("Database"=>$databaseName);
/* Connect using SQL Server Authentication. */
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn === false )
{
echo "Unable to connect.</br>";
die( print_r( sqlsrv_errors(), true));
}
echo "made sql connection";
return;
?>
sqlsrv_connect (sqlsrv extension) is not available in the GA branch, you need to get it from svn and compile it. You can also use the mssql_connect (mssql extension) or PDO.
I'm trying to connect to remote DB2 via PHP. But have some problems. I've already installed IBM Application developer client.
phpinfo() output:
IBM DB2, Cloudscape and Apache Derby support enabled
Module release 1.9.4
Module revision $Revision: 327944 $
Binary data mode (ibm_db2.binmode) DB2_BINARY
Then, I've got a php file which is looking like:
$database = 'MyDB';
$user = 'db2inst1';
$password = 'mypassword';
$hostname = '1.1.1.1';
$port = 50000;
$conn_string = "DRIVER={IBM DB2 ODBC DRIVER};DATABASE=$database;PORT=$port;HOSTNAME=$hostname;".
"PROTOCOL=TCPIP;UID=$user;PWD=$password;";
$conn = db2_connect($conn_string, '', '');
if ($conn) {
echo "connection to $database succeeded";
} else {
echo "connection to $database failed";
echo db2_conn_errormsg();
}
And trying to execute this file, I have "connection to MyDB failed", and NO visible response from db2_conn_errormsg(), which is actually making me baffled
Unfortunately, I haven't got a straight access to the remote server with database. But several months ago, when I was using other client, I succeeded to connect to exactly this database. But that time I didn't need to install IBM ADCL. That is why I can guess that problem is on this side. But even if so, I couldn't fix it.
Sorry if I duplicated some question on stackoverflow, but all answers, which I found, were unfortunately useless to me.
I'm using Apache 2.2 and PHP 5.4.
Hope you can help.
Thanks for any replies!
Are you sure you have connectivity to the server? Correct port, server, firewall rules, username, password, database name?
What is the SQL code you are receiving. Try to get the SQL code from PHP, "connection to xx failed" is your own code so it is useless to help you.
Did you install the application development client? which DB2 version are you using? ADCL is old, it was for DB2 8. Since DB2 9.7, clients have different names, and I think you need IBM Data server client in order to compile the php module. For more information, check this website: http://www-01.ibm.com/support/docview.wss?uid=swg27016878
I think you have to catalog the database server (node) and the database in the local machine with the db2 client. It seems that your PHP code uses ODBC driver, and it has to be configured locally.
Your connection string looks like an ODBC connection where as the db2_connect function in PHP needs :-
DATABASE=database;HOSTNAME=hostname;PORT=port;PROTOCOL=TCPIP;UID=username;PWD=password;
It's on the PHP web page.
I have never been able to get ibm_db2 or pdo_ibm working on a remote. They work if DB2 and Apache are on the same machine (like the iSeries) but not if the host is connecting to a remote DB2.
If you read the Doctrine2 PHP drivers for each you will find they redirect to ODBC if the host is not 'localhost' or '127.0.0.1'.
This code works for me
<?php
if (!$db = odbc_connect ( "AS400", $user, $password) )
echo 'error!';
$result = odbc_exec($db, "select count(*) from $table");
while (odbc_fetch_row($result)) {
var_dump($result);
print_r($result);
echo "\n";
echo odbc_result($result, 1)."\n";
}
odbc_close($db);
AS400 is DSN name defined in ODBC.