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.
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 an issue where using sqlsrv_fetch_array is returning NULL. I am trying to do a simple query on a view in MS SQL that I have verified in SQL Server Management Studio provides results.
I am running PHP 7.4.4 on Windows Server 2016 x64, with version 5.8 of the PHP SQL drivers and 17.5.2.1 of the ODBC driver.
Similar queries are being run on 4 other views within this database and all work fine. Below is the code I'm testing:
<?php
include("../mscon.php");
$msConnInfo = array( "Database"=>"XYZ", "UID"=>"username", "PWD"=>"password");
$msConn = sqlsrv_connect( $msServerName, $msConnInfo);
if($msConn)
{
//echo "Connection established.\n";
}
else
{
//echo "Connection could not be established.\n";
die( print_r( sqlsrv_errors(), true));
}
$query = "SELECT * FROM Public_Web";
$result = sqlsrv_query($msConn, $query, array(), array( "Scrollable" => 'static' )) or die( print_r( sqlsrv_errors(), true));
if ($result === false) {
die(print_r(sqlsrv_errors(), true));
}
else
echo "We good fam, no errors<br/>\n";
var_dump($result);
if(sqlsrv_has_rows($result))
{
echo "<br/>Rows exist<br/>\n";
$numRows = sqlsrv_num_rows($result);
echo "There are $numRows rows<br/>\n";
while ($row = sqlsrv_fetch_array($result));
{
var_dump($row);
}
}
else
{
echo "no results were found<br/>\n";
}
sqlsrv_free_stmt( $result);
sqlsrv_close($msConn);
?>
The above code outputs lets me know that there are no errors and that 1493 rows exist in the view. But when I do a var_dump on $row it outputs NULL. All of the fields in the view I'm trying to access are varchar and there are only 7 fields total.
I have tried just grabbing one field in my query instead of all fields, no change in results. It seems like I have tried all possible troubleshooting methods.
Anything you can think of would be greatly appreciated. Thanks.
Problem solved. We ended up deleting the view in SQL and recreating it. No idea why it wasn't working properly.
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;
}
I got the following script that returns me the data of a mssql databse, it works fine however special caracters like "ç" "á" "ã", etc is displayed in a weird way.
From what i saw, as it is a mssql this can be a pain.. didnt find any exact response.
I tried to add this line in order to fix:
ini_set('mssql.charset', 'UTF-8');
"Database"=>"programaplo","UID"=>"--","PWD"=>"--","CharacterSet"=>"UTF-8");
header('Content-type: application/json; Charset=UTF-8');
The script
<?php
error_reporting(1);
ini_set('mssql.charset', 'UTF-8');
$serverName = "mssql3.gear.host";
/* Get UID and PWD from application-specific files. */
$connectionInfo = array( "Database"=>"programaplo", "UID"=>"--", "PWD"=>"--","CharacterSet"=>"UTF-8");
$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));
}
$tsql = "SELECT * FROM Obras";
$stmt = sqlsrv_query($conn, $tsql);
if( $stmt === false ) {
echo "Error in executing query.</br>";
die( print_r( sqlsrv_errors(), true));
}
echo "Query: sucesso \n";
$json = array();
while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
$json[] = $row;
}
header('Content-type: application/json; Charset=UTF-8');
echo json_encode($json);
sqlsrv_free_stmt($stmt);
sqlsrv_close($conn); //Close the connnectiokn first
exit();
?>
The result:
{"Id":2,"NomeObra":"Super Olimpia","idCliente":"Associa\u00e7\u00e3o Super Olimpia","DataPLevantamento":"4 de agosto de 2016","DataRLevantamento":"4 de agosto de 2016","Estado":"Obra conclu\u00edda ","DataRMateriais":"6 de setembro de 2016","DataInicioObra":"18 de setembro de 2016","DataConclusao":"20 de outubro de 2016","DataVestoria":"18 de setembro de 2016","Obs":"","Prompor":"Gas Fenosa","Levantpor":"Ploran\/Paulo","executpor":"Ploran"}
The issue:
Associa\u00e7\u00e3o
Associa\u00e7\u00e3o is the official way to encode binary (or non-ASCII) characters in JSON.
Every Json parser [can|should] understand this notation.
See : JSON and escaping characters
my purpose is to display in a HTML table the result of a query to a remote ODBC SQL database with PHP.
I've already established the connection with the database, but when i try to execute a query and display it in a table it doesn't show anything.
Is there a difference in the query syntax if the table is a view?
The environment is WAMP installed on Windows, PHP 5.6.19, APACHE 2.4.18
This is the code (I have omitted the variables for the odbc connection):
<!DOCTYPE html>
<html>
<head>
<style>
table, th, td {
border: 1px solid black;
}
</style>
</head>
<body>
<?php
$conn=odbc_connect("Driver={SQL Server};Server=$server;Database=$database;", $user, $password);
if( $conn ) {
echo "Connection established.<br />";
}else{
echo "Connection could not be established.<br />";
die( print_r( odbc_error(), true));
$sql = "SELECT * FROM ASSET_VIEW";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "<table><tr><th>numero</th></tr>";
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<tr><td>".$row["N_IMMA"]."</td></tr>";
}
echo "</table>";
} else {
echo "0 results";
}
$conn->close();
}
?>
</body>
</html>
All I get is the "Connection established" string and that's it.
I would like to show all the results from the table (about 30 columns and 300 rows). I've tried with different tables but I still get the same result. I'm relatively new to PHP and MySql and maybe it's a silly request but I can't get my head around it for the moment.
Thank you
You haven't closed the else bracket for Connection could not be esatblished. That's why the Table part is not getting genearted.
if( $conn ) {
echo "Connection established.<br />";
}else{
echo "Connection could not be established.<br />";
die( print_r( odbc_error(), true));
}
// Logic to build the table from query