Cannot bring output rows of sql server into php using sqlsrv - php

Please note that i tried all existing solutions and still haven't got output.
I have a php page that calls a Stored Procedure in SQL Server using sqlsrv functions. I have tried existing solutions but unable to solve my problem. When i execute this same SP in SQL Management Studio, it gives output of 31 rows. However, here in my code i am unable to get any row of data. Could someone advise on how to?
Here is my code:
$serverName = "(local)"; //serverName\instanceName
// Server in the this format: <computer>\<instance name> or
// <server>,<port> when using a non default port number
$connectionInfo = array( "Database"=>"TESTDB");
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn ) {
$AcCode = '18100017';
$StartDate = '2016/12/01';
$EndDate = '2016/12/31';
$CutAmt = -50000;
$IntRate1 = 2;
$IntRate2 = 3;
$sql = "EXEC dbo.uspGetBankInterest #AC_NO= ?, #AsOfDate= ?, #EndDate= ? ,#CutAmt= ?, #IntRate1= ?, #IntRate2= ?";
$stmt = sqlsrv_prepare($conn, $sql, array(&$AcCode,&$StartDate,&$EndDate,&$CutAmt,&$IntRate1,&$IntRate2));
$result=sqlsrv_execute($stmt);
if( !$stmt ) {
die( print_r( sqlsrv_errors(), true));
}
if($result){
echo "SP Executed!";
}
$ctr=0;
$row_count = sqlsrv_num_rows( $stmt );
echo ' '.$row_count.'<br>';
while($row = sqlsrv_fetch_array($stmt,SQLSRV_FETCH_ASSOC))
{
print_r($stmt);
echo($row['STAT_DATE'].' '.$row['STAT_AMT'].' '.$row['INT_AMT']."<br>");
$ctr++;
}
echo 'The total number of records are:'.$ctr;
die();
}
else{
echo "Connection could not be established.<br />";
die( print_r( sqlsrv_errors(), true));
}
The output i get is:
SP Executed!
The total number of records are:0
When i executed, there was an output of 31 records and the data was displayed correctly. There is no problem in my SP. However, when i am trying it in php, no data is brought. How can i solve this problem?

Related

sqlsrv_query isn't working as expected

I'm in a vocational training right now and my task is to create a web-formula for accounting stuff. The server is running IIS 6.2 on Windows Server 2012 R2, PHP 7.2.1 and I finally got the MS PHP SQL Extension running.
Besides my code being a stackowerflow patchwork, it kinda works, only that I cant figure what I did wrong with the query. On submitting the formula it says Transaction rolled back, and I really don't know why. The query is, when pasted into MS SQL Server Management Studio, successfully executed, but not in the form.
$serverName = "server\sqlexpress";
$connOptions = array( "Database"=>"123", "UID"=>"456", "PWD"=>"xxx");
$conn = sqlsrv_connect( $serverName, $connOptions );
if( $conn === false ) {
die( print_r( sqlsrv_errors(), true ));
}
if ( sqlsrv_begin_transaction( $conn ) === false ) {
die( print_r( sqlsrv_errors(), true ));
}
/* This is the query */
$sql1 = "INSERT INTO Person (Kundennummer, Vorname, Nachname, Strasse, Hausnummer, Etage, Email) VALUES (?, ?, ?, ?, ?, ?, ?)";
$params1 = array($kundennummer, $vorname, $nachname ,$strasse, $hausnummer, $etage, $email);
$query1 = sqlsrv_query( $conn, $sql1, $params1 );
/* This is where it rolls back the transaction */
if($query1) {
sqlsrv_commit( $conn );
echo "Transaction committed.<br />";
}
else {
sqlsrv_rollback( $conn );
echo "Transaction rolled back.<br />";
}
sqlsrv_close($conn);
The Connection is working, because if i replace $sql1 with
$sql1 = "SELECT 'Hello world'";
it says commited.
Any guesses? Thanks alot, and, in case you seeing some more bad stuff happening in my code, feel free to correct me.
And suddenly i've got the answer.
When I added echo(print_r(sqlsrv_errors(), true)); to the code, it says "INSERT permission was denied on the object [...]", so I added permission to the user. Problem solved, thanks to this answer

"The active result for the query contains no fields" using MSSQL with PHP

Although this question has already been answered multiple times here, here, and here, none of these solutions could solve the issue for me.
I am trying to use PHP to fetch data from an MSSQL database. Although any query that I attempt to perform yields the same error message:
The active result for the query contains no fields.
Below is the code that I use to execute the query.
<?
include 'private.php'; // contains the servername, database, userID and password
$connectionInfo = array( "Database"=>$database, "UID"=>$userID, "PWD"=>$password);
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn ) {
echo "Connection established.<br />";
$query = 'USE data; SELECT * FROM Users;';
$stmt = sqlsrv_query( $conn, $query );
if(sqlsrv_has_rows($stmt))
{
echo 'success';
}
$row = sqlsrv_fetch_array( $stmt);
if ($row === false)
{
die( print_r( sqlsrv_errors(), true) );
}
while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC) ) {
echo $row['Name'].", ".$row['Value']."<br />";
}
sqlsrv_free_stmt( $stmt);
}
else {
echo "Connection could not be established.<br />";
die( print_r( sqlsrv_errors(), true));
}
?>
Surprisingly, executing the same query in Microsoft SQL Server Management Studio works without any errors and executing queries that do not return information work fine aswell.
Note that I have already tried using SET NOCOUNT ON;, which is recommended in some answers regarding the same topic.
How can I resolve this error?

PHP Script cant see the result of a query in json format, no errors displayed

I am trying to get the values of a table from a MSSQL Database hosted online, however, I cant get any data from it, I already looked several times in the code and cant find a error or some mis-type in the code.
I get both echos confirming connection and query were executed but then after that i should be able to see the json, but nothing is displayed, I am sure the query is correct because I use it in other places.
NOTES:
I have the sqlsrv PHP extension
The script:
<?php
error_reporting(1);
$serverName = "...";
/* Get UID and PWD from application-specific files. */
$uid = '...';
$pwd = '...';
$connectionInfo = array( "UID"=>$uid,
"PWD"=>$pwd,
"Database"=>"programaplo"
);
/* Connect using SQL Server Authentication. */
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn === false )
{
echo "Unable to connect.</br>";
die( print_r( sqlsrv_errors(), true));
}
echo "Conexão: sucesso \n";
$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_ASSOC($stmt)) {
$json[] = $row;
}
/* Run the tabular results through json_encode() */
/* And ensure numbers don't get cast to trings */
echo json_encode($json);
/* Free statement and connection resources. */
sqlsrv_free_stmt( $stmt);
sqlsrv_close( $conn);
?>

Basic data retrieval using $row

I am using PHP 5.6 and SQL Server 2012 with a nonthreadsafe driver. I have an incredibly easy question I already know, but for some reason i am completely brain dead.
I have a basic SQL query that will can either return 1 row of data, or multiple.
There is only three fields with data. but I would like to be able to read off the data (this is going into a vXML ivr using Voice Server 4.0, but that is irrelevant to my question).
Here is my code:
<?php
$serverName = "localhost";
$connectionOptions = array("Database"=>"mydb");
/* Connect using Windows Authentication. */
$conn = sqlsrv_connect( $serverName, $connectionOptions);
if( $conn ) {
echo "Connection established.";
}else{
echo "Connection could not be established.";
die( print_r( sqlsrv_errors(), true));
}
$sql = "SELECT * FROM my_table WHERE SSN = 111111111";
//////I have no idea if I need this or not input appreciated
$stmt = sqlsrv_query( $conn, $sql );
if( $stmt === false ) {
die( print_r( sqlsrv_errors(), true));
}
?>
All I would like to do is be able to retreive the information as rows, like
echo $row['field'];
echo $row['field2'];
echo $row['field3'];
ect.
would someone be able to point me in the right direction? I can only find mysqli examples, and those do not seem to work. Thanks!
I assume what you're looking for is sqlsrv_fetch_array
sqlsrv_fetch_array — Returns a row as an array
An Example:
$stmt = sqlsrv_query( $conn, $sql );
if( $stmt === false ) {
die( print_r( sqlsrv_errors(), true));
}
while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC) ) {
echo $row['field'].", ".$row['field2']."<br />";
}
PHP Manual: sqlsrv_fetch_array

PHP Date Parameters with SQLSRV

I'm learning PHP and having it talk to our SQL Server 2008R2 DB. I am trying to run a query that has 2 date parameters that need to pass in. I know I could code the date params in to the SQL but I would like to see what I am doing wrong with the placeholder method below:
$serverName = "server";
$connectionInfo = array( "Database"=>"db", "UID"=>"user", "PWD"=>"pwd");
$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));
}
$startDate = strtotime("2015-04-30");
$endDate = strtotime("2015-05-01");
$sql = "SELECT * FROM history
WHERE (ts >= ? and ts < ?)
ORDER BY ts";
$params = array($startDate, $endDate);
$stmt = sqlsrv_query($conn, $sql, $params);
if ($stmt == false) {
die( print_r( sqlsrv_errors(), true));
}
while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
echo "<p>" . $row['vesselname'] . "</p>";
}
sqlsrv_free_stmt($stmt);
sqlsrv_close($conn);
This returns the error "Arithmetic overflow error converting expression to data type datetime". ts is a datetime field.
Never mind. I figured out that I don't need the strtotime and to just pass date strings.

Categories