Connection Strings in Laravel - php

In Laravel, I set my SQL Server connection string like this in the .env file: -
DB_CONNECTION=mssql
DB_HOST=sql-server123.database.windows.net
DB_PORT=1433
DB_DATABASE=dev-db-123
DB_USERNAME=user123
DB_PASSWORD=pass123
But now I'd like to format my connection string to connect to SQL Azure using the system-assigned managed identity provided by Azure Cloud.
As a test, I used Microsoft's example to connect using a System Managed Identity: -
https://learn.microsoft.com/en-us/sql/connect/php/azure-active-directory?view=sql-server-ver15
<?php
$azureServer = 'myazureserver.database.windows.net';
$azureDatabase = 'myazuredatabase';
$connectionInfo = array('Database'=>$azureDatabase,
'Authentication'=>'ActiveDirectoryMsi');
$conn = sqlsrv_connect($azureServer, $connectionInfo);
if ($conn === false) {
echo "Could not connect with Authentication=ActiveDirectoryMsi (system-assigned).\n";
print_r(sqlsrv_errors());
} else {
echo "Connected successfully with Authentication=ActiveDirectoryMsi (system-assigned).\n";
$tsql = "SELECT ##Version AS SQL_VERSION";
$stmt = sqlsrv_query($conn, $tsql);
if ($stmt === false) {
echo "Failed to run the simple query (system-assigned).\n";
print_r(sqlsrv_errors());
} else {
while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
echo $row['SQL_VERSION'] . PHP_EOL;
}
sqlsrv_free_stmt($stmt);
}
sqlsrv_close($conn);
}
?>
Where in Laravel can I set the connection string to use: -
'Authentication'=>'ActiveDirectoryMsi'

Try this and let me know:
DB_CONNECTION=mssql
DB_HOST=sql-server123.database.windows.net
DB_PORT=1433
DB_DATABASE=dev-db-123
DB_USERNAME=user123
DB_PASSWORD=pass123
AUTHENTICATION=ActiveDirectoryMsi
or in alternative:
DB_AUTHENTICATION=ActiveDirectoryMsi

Related

TCP Provider: Error code 0x2746 / No Connectivity via PHP and Apache2

I've installed the ODBC Drivers for Microsoft SQL Server on Debian 11 and configured everything appropriately. When I call the PHP script via
php /var/www/html/sqlsrv.php
everything is working as excepted. When calling the PHP via browser I am constantly getting the following error
SQL Error: Error information: SQLSTATE: 08001 Code: 10054 Message:
[Microsoft][ODBC Driver 18 for SQL Server]TCP Provider: Error code
0x2746 SQLSTATE: 08001 Code: 10054 Message: [Microsoft][ODBC Driver 18
for SQL Server]Client unable to establish connection
Here is my PHP so far:
<?php
$serverName = "xxx\SESQL";
$connectionOptions = array(
"database" => "SEMESS",
"uid" => "xxx",
"pwd" => "xxx",
"TrustServerCertificate" => "yes"
);
// Establishes the connection
$conn = sqlsrv_connect($serverName, $connectionOptions);
if ($conn === false) {
die(formatErrors(sqlsrv_errors()));
}
// Select Query
$tsql = "SELECT ##Version AS SQL_VERSION";
$tsql2 = "SELECT * FROM BENUTZER";
// Executes the query
$stmt = sqlsrv_query($conn, $tsql);
$stmt2 = sqlsrv_query($conn, $tsql2);
// Error handling
if ($stmt === false) {
die(formatErrors(sqlsrv_errors()));
}
?>
<h1> Success Results : </h1>
<?php
while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
echo $row['SQL_VERSION'] . PHP_EOL;
}
while( $row = sqlsrv_fetch_array( $stmt2, SQLSRV_FETCH_ASSOC) ) {
var_dump($row);
}
sqlsrv_free_stmt($stmt);
sqlsrv_close($conn);
function formatErrors($errors)
{
// Display errors
echo "<h1>SQL Error:</h1>";
echo "Error information: <br/>";
foreach ($errors as $error) {
echo "SQLSTATE: ". $error['SQLSTATE'] . "<br/>";
echo "Code: ". $error['code'] . "<br/>";
echo "Message: ". $error['message'] . "<br/>";
}
}
?>
I've added
"TrustServerCertificate" => "yes"
as without it a connection was not possible.
Also, I've updated to OpenSSL 1.1.1 - do I need to update Apache as
well? If, how?
Why is it working with PHP CLI but not with PHP Apache?

query with space in name

I have a database that is allready setup and can not be changed due to ERP software running on it. When trying to connect to retrieve some data into a table in PHP, i get the following error:
[SQL Server]Invalid object name 'Lareco Live'
I understand that this is due to the space in the tablename --> [Lareco Live$Lead]
This is my script:
// Establishes the connection
$dbh = sqlsrv_connect($serverName, $connectionOptions);
if ($dbh === false) {
echo "Error (sqlsrv_connect): ".print_r(sqlsrv_errors(), true);
exit;
}
$sql = "select No_, Description from [Lareco Live$Lead] with (nolock) ";
$getResults = sqlsrv_query($dbh, $sql);
if ($getResults === false) {
echo "Error (sqlsrv_query): ".print_r(sqlsrv_errors(), true);
exit;
}

php(ssh2, tunnel), I can't connect my remote mysql server

<?
$server_ip = "my ubuntu ip";
$server_port = "my ubuntu port";
$server_user = 'my id';
$user_pw = 'my pw';
$connection = ssh2_connect($server_ip, $server_port);
if (ssh2_auth_password($connection, $server_user, $user_pw)) {
echo "Authentication Successful!<br>";
} else {
echo "Authentication failed...";
}
$mysql_ip = '10.41.12.71';
$mysql_default_port = '3306';
$tunnel = ssh2_tunnel($connection,$mysql_ip,$mysql_default_port);
if($tunnel) {
echo "tunnel created<br>";
}else {
echo "tunnel creation failed";
die();
}
$mysql_user = 'root';
$mysql_user_pw = 'tkakcy159*';
$dbname = 'test';
// $dbconn = mysqli_connect($tunnel,$mysql_user,$mysql_user_pw,$dbname);
$dbconn = new mysqli($tunnel, $mysql_user, $mysql_user_pw, $dbname);
if ($dbconn) {
echo $dbname." connected<br>";
} else {
echo "DBConnection failed: " . $dbconn->connect_error;
}
// $insert_sql = " insert into test_table values('0001','wlsdhks0423'); ";
// $result = mysqli_query($dbconn,$insert_sql);
// if($result) {
// echo "data insert success<br>";
// }
// else {
// echo "data insert failed : ".mysqli_error($dbconn);
// }
$select_sql = " select * from test_table; ";
$result = mysqli_query($dbconn,$select_sql);
if($result) {
echo "selected rows : ".mysqli_num_rows($result)."<br>";
$row = mysqli_fetch_array($result);
$key = $row['test_key'];
$value = $row['test_value'];
echo "key = ".$key."<br>value = ".$value;
}else {
echo mysqli_error($dbconn);
}
// phpinfo();
?>
This is my code. mysql is on ubuntu device.
And I think I can access ubuntu, but mysql connection is failed
this php's output is
Authentication Successful!
tunnel created
test connected
and if i use this line
$dbconn = mysqli_connect($tunnel,$mysql_user,$mysql_user_pw,$dbname);
then output is
Authentication Successful!
tunnel created
DBConnection failed:
The obvious thing is that $connection and $tunnel has no problem.
I had tested them. And works well.
This is an exact duplicate of the postgresql question because they both exist for the same reason. You're both trying to connect a service over a SSH Tunnel by using ssh2_tunnel incorrectly.
ssh2_tunnel returns a resource aka a file pointer
The ssh2_tunnel() return a socket object which means you can operate this socket object just like you use standard socket function.
I have such code:
fwrite($ssh_tunnel, $db_password);
while (!feof($ssh_tunnel)) {
echo fgets($ssh_tunnel, 1280);
}
and I get such result:
a 5.5.5-10.3.34-MariaDB-cll-lve��=^Aile5x����}.hBNEM5_^:Gmysql_native_password!��#08S01Got packets out of order
I think fwrite can send something to such tunnel we have created, so I am currently figuring out what data should be sent to.

Why I can not connect to Oracle 10g from a PHP program?

I am using Oracle 1Og express edition and EasyPHP v5.3.5.0
My oracle database in running on port 8080.
I can not connect the oracle database using my php program.
I am confused about the 3rd parameter of the oci_connect statement.
Here is my code.
<?php
$conn = oci_connect("scott", "tiger","localhost/XE");
if (!$conn)
{
$m = oci_error();
echo $m['message'], "\n";
exit;
}
else
{
print "Connected to Oracle!";
}
oci_close($conn);
?>
Can you try this method instead? (granted you know your tnsnames entry)
<?php
$tns = "(DESCRIPTION=(ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = 127.0.0.1)(PORT = 1521)))(CONNECT_DATA=(SID=XE)))" ;
if($conn = OCILogon("scott", "tiger", $tns))
{
echo “Connected.\n”;
OCILogoff($conn);
}
else
{
$err = OCIError();
echo “Connection ERR.” . $err[text] . "\n";
}
?>

mysql_connect doesn't return anything

Heres my code. Simple.
<?php
echo 'start<br>';
//Do the conntection
$checkconnection = mysql_connect('localhost', 'root', 'rootpass');
//Check if it's valid
if(!$checkconnection) {
echo 'CheckFailed';
} else{
echo 'CheckSucess';
}
echo 'end'; ?>
but I only can see 'start'. There is no 'CheckFailed', 'CheckSucess', 'end'
What should I do?
I already install mysql, create database, create tables, of course.
<?php
// Create connection
$con=mysqli_connect("localhost","root","root","database");
// Check connection
if (mysqli_connect_errno($con))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
return false;
}
$result = mysqli_query($con, "SELECT * FROM table;");
?>

Categories