Why PHP can't use Sqlsrv_connect with cmd - php

I make sure that I can run website with WampServer,and also can connect the Sql Server.However,I run the bat file by the operating system scheduler,that return error:
OK,Can Run PHP
Fatal error: Uncaught Error:Call to undefined function sqlsrv_connect() in C:\wamp64\www\shell.php
Stack trace:
#0{main}
thrown in C:\wamp64\www\shell.php
My configuration : WampServer 3.0.6 64bits (PHP 7.0.10, Apache 2.4.23)
There are my steps:
First: I wrote a bat file in \wamp64\bin\php\php7.0.10
C:\wamp64\bin\php\php7.0.10\php.exe -q C:\test.php
PAUSE
Second: The function sql_connect is basic on php
echo "OK,Can Run PHP";
$serverName = "------";
$uid = "------";
$pwd = "------";
$connectionInfo = array("UID"=>$uid, "PWD"=>$pwd, "Database"=>"------","characterset"=>"utf-8");
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn == false){
echo "Error<br>";
die( print_r( sqlsrv_errors(), true));
}
else{
echo "OK<br>";
}
Third: Put the bat in the operating system scheduler(Win7 64bit)
The cmd also can work by php -v and php -f.I think that cmd can work PHP with bat on the operating system scheduler because it answer "OK,Can Run PHP".
And then,the problem maybe is in "sqlsrv_connect()".
However,I really don't know that why the function "sqlsrv_connect()" can work in localhost website but can't work cmd without website. Please help me!!

Related

How do I setup sqlsvr in PHP 8.1 and above?

Currently I have PHP 8.1.14,and right now when I try to use sqlsrv_connect() it will only show me this error:
Uncaught Error: Call to undefined function sqlsrv_connect().
After some researching I realized that I need to download some extensions for my PHP in order to use it.
The two .dll I've downloaded are and placed in my C:\xampp\php\ext folder are:
php_pdo_sqlsrv_81_ts_x64.dll and php_sqlsrv_81_ts_x64.dll
Which I am not certain if I have downloaded the correct one.
I have also added these extensions in the php.ini file under the Dynamic Extension section.
;extension=pdo_pgsql
extension=pdo_sqlite
;extension=pgsql
;extension=shmop
extension=php_pdo_sqlsrv_81_ts_x64.dll
extension=php_sqlsrv_81_ts_x64.dll
Also, these are the line of codes that i used to call sqlsvr_connect() function
<?php
$serverName = " **server name**, **Port Number**"; //serverName\instanceName, portNumber (default is 1433)
$connectionInfo = array( "Database"=>"**Database**", "UID"=>"**user ID**", "PWD"=>"**Password**");
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn ) {
echo "Connection established.<br />";
}else{
echo "<script>alert('Could not establish connection!');
";
die("window.history.go(-1);</script>");
}
?>
Would highly appreciate any help regarding this matter! Thank you in advance.

Fatal error: Uncaught Error: Call to undefined function db2_connect()

I have some problem. I have been looking it for almost a week. I have done include ibm_db2.dll, change php version but not working. When I run the db2 query, it shows Fatal error: Uncaught Error: Call to undefined function db2_connect() in C:\laragon\www\hpc_dev\db2_conn.php:15 Stack trace: #0 C:\laragon\www\hpc_dev\index.php(3): include() #1 {main} thrown in C:\laragon\www\hpc_dev\db2_conn.php on line 15
Currently I am using php version 7.4.12 64bit (ts) using laragon in Windows 10. I also have tested using Xampp and the same problem occur. Below are my codes to connect to ibm db2:
db2_conn.php:
<?php
ini_set("display_errors", 1);
$database = 'DB2';
$user = 'xxx';
$password = 'xxx';
$hostname = 'xx.xx.xx';
$port = 60000;
$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 {
echo "Connection failed.";
}
?>
My code to show data from db2:
<?php
include("db2_conn.php");
$sql = "SELECT * FROM EMPLOYER WHERE EMPR_LOGIN_NAME = 'xxxxx'";
if ($conn)
{
$stmt = db2_exec($conn, $sql);
$row = db2_fetch_assoc($stmt);
echo $row['SECTOR_DESC'];
db2_close($conn);
}
?>
I don't know either the problem comes from db2 extension or others. Please help me.
For information, I have download the ibm db2 extension here: https://github.com/ibmdb/php_ibm_db2
Thanks
Your question omits details of the command-lines and actions you performed.
Most likely you missed a step in the configuration.
Remember to open the laragon terminal and verify that the Db2 driver is loaded:
php -m | grep ibm
should return ibm_db2 if you have correctly configured the correct version of the extension.
If you do not see that the ibm_db2 extension is loaded then any db2_connect will fail until you resolve the missing module.
For information the current laragon_full (November 2020) delivers PHP 7.2 TS, and that works correctly with the php_ibm_db2.dll TS for v7.2.x "out of the box" (which you must first download and configure).
Additionally, if I upgrade the PHP version for laragon to PHP 7.4.12 TS, and subsequently download and configure the matching php_ibm_db2.dll TS for php v7.4.x, then it all "just works" without any issues on Win 10 x64 2020 to connect to Db2-LUW and run queries etc.
It helps to verify that your Db2 client is correctly configured. You can do that independently of PHP. If you are using the clidriver Db2-client (the smallest footprint driver) you can configure the db2dsdriver.cfg XML file and then run db2cli validate -connect -dsn -user ... -passwd ... to verify that the connection works. If you are using either the Db2-runtime-client or the Db2 full client, or a local Db2-server, you can run (in the laragon terminal):
(after configuring your Db2 node directory and Db2 db directory via appropriate configuration commands )::
set DB2CLP=**$$**
db2 connect to $yourdsn user ... using ...

Call to undefined function sqlsrv_connect() - Troubleshooting

System Information
CMS: Wordpress
Web Server: XAMPP
PHP Version: 5.5.30
MS Management Studio 17
Goal
Establish MSSQL Database connection using PHP
What has been done
Downloaded SQLSRV Drivers
Copied files php_pdo_sqlsrv_55_nts.dll and php_pdo_sqlsrv_55_ts.dll to the directory C:\xampp\php\ext
Added the following lines to the dynamic extensions part in the php.ini file: extension=php_pdo_sqlsrv_55_ts.dll and extension=php_pdo_sqlsrv_55_nts.dll
Restarted Web Server
Confirmed sqlsrv is listed in phpinfo()
Code
$serverName = "technology-pc\sqlexpress";
// The connection will be attempted using Windows Authentication.
$connectionInfo = array( "Database"=>"example_db");
$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));
}
Error
Call to undefined function sqlsrv_connect()
You have added the PDO variant of SQLSRV drivers to the extension list, but have not added the base drivers php_sqlsrv_55_ts.dll.
Add to the php.ini:
extension=php_sqlsrv_55_ts.dll
or
extension=php_sqlsrv_55_nts.dll
Also, you really should be using either the Thread-Safe (_ts.dll) or Non-Thread-Safe (_nts.dll) versions of the driver, not both. I believe that, as you are using an Apache Server, you should be using the Thread-Safe versions. So you php.ini should have:
extension=php_sqlsrv_55_ts.dll
extension=php_pdo_sqlsrv_55_ts.dll
You probably edited the wrong php.ini file. Check the right php.ini file with php info.
You can use this script:
<?php echo phpinfo(); ?>
Or if you have CLI access type php -i to get the info listed.
Check for the right path of your php.ini file.
Try below code to connect mssql database
$server = 'dburl.com\MSSQLSERVER, 1433';
$username = 'uname';
$password = 'password';
$connectionInfo = array( "Database"=>$database, "UID"=>$username, "PWD"=>$password,"ReturnDatesAsStrings"=>true);
$conn = sqlsrv_connect( $server, $connectionInfo);

SSH in xampp install php

I need to use a connection SSH and PHP, I have version PHP 5.6.8 ,
intall OpenSSL-Win32;
In php.ini :
extension=libssh2.dll ;librerias ssl2
extension=php_ssh2.dll
The libraries are in the folder php---->ext.
With the next program my result is:
Fatal error: Call to undefined function ssh2_connect() in
C:\xampp\htdocs\conexion\conexion_ssh.php on line 2
<?php
$connection = ssh2_connect('X.X.X.X', XX);
if (ssh2_auth_password($connection, 'root', 'XXXXXXXX')) {
echo "Authentication Successful!\n";
} else {
die('Authentication Failed...');
}
?>
Do you know the problem? I think is the configuration the system variable PATH but I don't Know the process.
Thanks for you help me!

i get a Fatal error when i try to connect mssql using php in wampserver

I'm using Wampserver (32bit & php 5.4I)2.4 *in windows 64bit*, will i install the 32bit becuse i need a connection to the MSSQL which required that as i know so far.
i downloaded the Microsoft Visual C++
and the php is a thread safe
so i added the two .dll files (php_pdo_sqlsrv_54_ts , php_sqlsrv_54_ts*) to the C:\wamp\bin\php\php5.4.16\ext*, then added them in the php.ini ( extension=php_pdo_sqlsrv_54_ts and extension=php_sqlsrv_54_ts), then restarted the wampserver
finally i gett an error says "Fatal error: Call to undefined function sqlsrv_connect()"
the code that i use is :
<?php
$serverName = "serverName\instanceName";
$connectionInfo = array( "Database"=>"DB NAME");
$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));
}
?>
could anyone tell me what did i do wrong to cause this error?
Use phpinfo() to see if the extension was loaded correctly.
If yes, see the error log for any issues encountered when starting apache (W*A*MP).
If yes, then the extension binary is wrong or you forgot to enter the file extension (.dll on Win, .so on Linux) in the php.ini.

Categories