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;
}
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
<?
$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 doing a little bit of learning on mysql, php and the like. I'm using a shared hosting plan so am quite limited from a settings changes point of view.
I am attempting to run a simple mysql select command through PHP, but all i get back is a blank error
<?php
$typeID = $_GET['tid'];
//variables for the database server
$server = "localhost";
$user = "codingma_rbstock";
$pwd = "M#nL%V{%RI+h";
$db = "codingma_rbstock";
//variables for the database fields
$itemNo;
$itemNm;
$itemDesc;
$buyPr;
$sellPr;
$quan;
$dept;
//database connection
//create connection
$conn = new mysqli($server, $user, $pwd, $db);
//if the connection fails throw an error.
if ($conn->connect_error){
die("Connection Failed: " . $conn->connect_error);
}
echo "Welcome to " . $typeID . "<br>";
$sql = "select ITEM_NAME from stock where ITEM_NO='00001'";
if ($conn->query($sql) === TRUE){
$res = $conn->query($sql);
if ($res->num_rows > 0){
echo "success";
}
}else{
echo "Error: " .$sql . "<br>" . $conn->error;
}
echo $res;
?>
I have checked and it seems to be connecting to the database fine (I changed a few account details to see if that threw a different error and it did).
I am sure I am missing something completely obvious here! The below is the text output from the error;
Error: select ITEM_NAME from stock where ITEM_NO='00001'
Thanks for any help.
your problem is in this line
if ($conn->query($sql) === TRUE){
you are doing a variable type check ( === ), the result of that comparision will always fail because, for as long as you have data in your table and your query doesn't fail $conn->query($sql) will not return a boolean value
mysqli::query documentation says:
Returns FALSE on failure. For successful SELECT, SHOW, DESCRIBE or EXPLAIN queries mysqli_query() will return a mysqli_result object. For other successful queries mysqli_query() will return TRUE.
You are using here a SELECT, therefore a successfull result won't be boolean
Try switching to
if ($conn->query($sql) == TRUE){
Or even better remove that if completely
EDIT
The better approach for that part of the code is:
$res = $conn->query($sql);
if ($res->num_rows > 0){
echo "success";
}
if ($res === false) {
echo "Error: " .$sql . "<br>" . $conn->error;
}
When I am writing this code, I am getting a fatal error stating:
Call to a member function query() on a non-object in
C:\wamp\www\demo.php on line 27.
How can I get rid of this error?
<!DOCTYPE html>
<html>
<body>
<?php
$searchtype = $_POST["searchtype"];
$searchterm = $_POST["searchterm"];
$searchterm = trim($searchterm);
if(!$searchtype && !$searchterm) {
echo "You have not entered search details.";
}
$mysqli = new mysqli("localhost","root","","books");
if($mysqli = false) {
echo "ERROR:Sorry,Could Not Connect To The Database.";
} else {
echo "Connected To Database";
}
$sql = "SELECT author FROM books";
if($mysqli -> query($sql)) {
echo "Connected To Tables";
} else {
echo "Cannot connect tot tables right now.";
}
?>
</html>
if ($mysqli = false)
This line is assigning the boolean value false to $mysqli. Change it to:
if ($mysqli == false)
or (better yet) if ($mysqli === false).
To prevent this error in the future, I recommend you employ Yoda conventions. In other words:
if (false === $mysqli)
It is:
if ($mysqli->connect_errno) {
printf("Connect failed: %s\n", $mysqli->connect_error);
exit();
}
as per http://www.php.net/manual/en/mysqli.query.php
Also the query execution need to change to
if($mysqli->query($sql)) {
In your code you check
if($mysqli = false)
when what you want is
if($mysqli == false)
However, this will not work if there is a connection problem as you will still get a mysqli instance back. You're looking for
if($mysqli->connect_error)
See here for more info
Why won't this table get Truncated? No error is returned. If I run the truncate SQL in SQL Server Management Studio, it truncates normally.
$truncate = "USE energyDB truncate table temp_energydata";
// Initiate connection to energyDB MS SQL server
$connection = sqlsrv_connect($serverName, $connectionInfo);
if( $connection ) {
echo "Connection established.";
}else{
echo "Connection could not be established.<br />";
die( print_r( sqlsrv_errors(), true));
}
// Start the query on the Database server
$statement = sqlsrv_query($connection,$truncate);
if( $statement ) {
echo "Truncate Query completed.";
}else{
echo "Query not completed.<br />";
die( print_r( sqlsrv_errors(), true));
#user1745767
$truncate = "USE energyDB truncate table [temp_energydata]";
try like this it tooks hours for met too.