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?
Related
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
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;
}
<?
$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.
I am getting the following error when I'm trying the code down below:
Error: Warning: mysqli::mysqli(): (HY000/2002): No such file or directory in /volume1/web/index.php on line 4 Warning: mysqli::query(): Couldn't fetch mysqli in /volume1/web/index.php on line 7 Warning: main(): Couldn't fetch mysqli in /volume1/web/index.php on line 23
The code that I am using..
<?php
// Connect to the DB
$mysqli = NEW MySQLi("localhost","root","","dbproject");
//Query the DB
$resultSet = $mysqli->query("SELECT * FROM clients");
// Count the returned rows
if($resultSet->num_rows != 0) {
while($rows = $resultSet->fetch_assoc())
{
$id = $rows['id'];
$fname = $rows['firstname'];
$lname = $rows['lastname'];
$country = $rows['country'];
echo "<p>ID: " .$id. " <br /> Name: ".$fname." ".$lname. "<br /> Country: ".$country." </p>";
}
// Turn the results into an array
// Display the results
} else{
echo $mysqli->error;
}
?>
As the error reffers to line 4, 7 and 23
Line 4:
$mysqli = NEW MySQLi("localhost","root","","dbproject");
Line 7:
$resultSet = $mysqli->query("SELECT * FROM clients");
Line 23:
echo $mysqli->error;
Can someone please help me with this? Many thanks!
The solution:
I changed line 4 from:
$mysqli = NEW MySQLi("localhost","root","","dbproject");
to
$mysqli = NEW MySQLi("localhost:3307","root","","dbproject");
Adding the port somehow fixed it.
I just tried it on my server and I don't see anything wrong with it. Just make sure your login details are correct as wel as you database and table name.
This is the script I just tested on my server. I added a connection test and changed the database and table name:
<?php
// Connect to the DB
$mysqli = NEW MySQLi("localhost","root","","test");
// Check connection
if ($mysqli->connect_error) {
die("Connection failed: " . $mysqli->connect_error);
}
//Query the DB
$resultSet = $mysqli->query("SELECT * FROM testing");
// Count the returned rows
if($resultSet->num_rows != 0) {
while($rows = $resultSet->fetch_assoc())
{
$id = $rows['ID'];
echo "<p>ID: " .$id. " </p>";
}
// Turn the results into an array
// Display the results
} else{
echo $mysqli->error;
}
?>
The script below connects to the db (I get the connected successfully echo) but none of the data from the query is shown onscreen.
I assume the data must be somewhere as I do not get the error message.
Question: Where is the error in the script?
<?php
//connectdb();
$con = mysqli_connect("localhost","UN","PW");
if ( $con == "" ) { echo " DB Connection error...\r\n"; exit(); }
echo 'Connected successfully';
$result = mysqli_query($con, "SELECT graduation_year FROM wp_gfsept2013");
while($row = mysql_fetch_array($result))
if ($result === "") {echo "An error occurred.";}
{
echo $row['graduation_year'];
echo "<br>";
}
?>
Appreciate any help that can be sent my way, I'm a real newbie at this stuff.
Roger
Try adding an opening brace after while($row = mysql_fetch_array($result)) and a closing brace before the end of the script.
Is this not a syntax issue?? Why is there an IF clause after a WHILE clause but before the opening bracket for the WHILE loop block?
Additionally, you are trying to use mysql_fetch_array() instead of mysqli_fetch_array().
<?php
//connectdb();
$con = mysqli_connect("localhost","UN","PW");
if ( $con == "" ) { echo " DB Connection error...\r\n"; exit(); }
echo 'Connected successfully';
$result = mysqli_query($con, "SELECT graduation_year FROM wp_gfsept2013");
if ($result !== FALSE && mysqli_num_rows($result) > 0) { // Proper way to test for results
while($row = mysqli_fetch_assoc($result))
{
echo $row['graduation_year'];
echo "<br/>";
}
}
else {
die("Query Returned 0 rows...");
}
?>
Documentation: mysqli_result::$num_rows