i took below SP HIt using sqlsrv and PHP. Snippet does not worked for me. For the below code i am getting " SQL SRV QUERY NOT RAN ". please provide your advise, how to achieve php + sqlsrv + SP.
$query = "{CALL call_user_name}";
if ( ($res = sqlsrv_query( CONNSTR, $query)) )
{
do
{
if ( sqlsrv_num_fields($res) ) // one way to check if results returned.
{
while( ($row = sqlsrv_fetch_array( $res, $type)) )
{
$data[] = $row;
}
}else{echo "sqlsrv_num_fields not working";}
} while ( sqlsrv_next_result($res) ) ;
sqlsrv_free_stmt($res); // not essential, but good form if your script does lots of other stuff.
}else{
echo "SQL SRV QUERY NOT RAN ";die( print_r( sqlsrv_errors(), true));
}
There are some possible causes for your error, my suggestion: change this line
echo "SQL SRV QUERY NOT RAN"
to
die( print_r( sqlsrv_errors(), true));
Then you can see what is causing the error.
Sample PHP SQL Srv stored Procedure call (With parameters). Hope this will help you to get an idea. (Used to call this php file through Ajax request and Return data after calling the SP)
smeSelectDrp is the SP Name
$sql = "EXEC smeSelectDrp";
$stmt = sqlsrv_prepare($conn, $sql);
if (!sqlsrv_execute($stmt)) {
echo "Error Retrieving Data";
die;
}
else
{
while($row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_NUMERIC) ) {
$value[] = $row;
}
echo json_encode($value);
}
Related
SQL Stored Procedure returns NULL when I call it from my mobile app or a web browser, but the same procedure returns what it has to when called using SQLPro for MSSQL software
if( $conn == FALSE ) {
echo "Connection failed.";
die( print_r( sqlsrv_errors(), true));
}
$query = "EXEC dbo.sp_Pok_Details #oe=17,#code='5907769000409'";
$getProducts = sqlsrv_query($conn, $query);
if ($getProducts == FALSE)
{
die(FormatErrors(sqlsrv_errors()));
}
while($row = sqlsrv_fetch_array($getProducts, SQLSRV_FETCH_ASSOC))
{
$returnArray[] = $row;
}
echo json_encode($returnArray);
Explanations:
You need to put SET NOCOUNT ON as first line in your stored procedure to prevent returning the number of rows affected by the T-SQL statements as part of the result set. This is the reason for your NULL results.
If you can't change your stored procedure, use sqlsrv_next_result() to make the next result active and then fetch the data.
As a note, always use prepared statements and parameterized queries to prevent SQL injection. With PHP Driver for SQL Server, function sqlsrv_query() does both statement preparation and statement execution and can be used to execute parameterized queries.
Example (based on your code):
<?php
if( $conn === false ) {
echo "Connection failed.";
die( print_r( sqlsrv_errors(), true));
}
$query = "EXEC dbo.sp_Pok_Details #oe = ?, #code = ?";
$params = array(17, '5907769000409');
$getProducts = sqlsrv_query($conn, $query, $params);
if ($getProducts === false) {
die( print_r( sqlsrv_errors(), true));
}
// Just for test - make additional call(s) to get the count of the active result sets
do {
while($row = sqlsrv_fetch_array($getProducts, SQLSRV_FETCH_ASSOC)) {
echo 'Current rowset'.'<br>';
}
} while (sqlsrv_next_result($getProducts));
// Or if you have only one result set
//while ($row = sqlsrv_fetch_array($getProducts, SQLSRV_FETCH_ASSOC)) {
// $returnArray[] = $row;
//}
echo json_encode($returnArray);
?>
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?
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
I have a stored procedure with 2 parameters. I'm able to execute stored procedures with one parameter using the same script below. But I couldn't make it work with two parameters.
$stmt = "{CALL VM_GETPRs_CAMPS (?,?)}";**//SP has 160 rows of data.**
$fdate=date("Y-m-d");
$tdate=date("Y-m-d");
$params = array(
array($fdate,SQLSRV_PARAM_IN),
array($tdate,SQLSRV_PARAM_IN)
);
$result = sqlsrv_query( $conn, $stmt,$params,array('Scrollable' => 'buffered')); //not getting any error
if( $result === false) {
die( print_r( sqlsrv_errors(), true) );
} else{
**//**I tried sqlsrv_num_rows and sqlsrv_has_rows both are giving zero rows.**
$row_count = sqlsrv_num_rows( $result );
if ($row_count === false)
echo "No rows";
else if ($row_count >=0)
echo "\n$row_count\n";
if(sqlsrv_has_rows($result))
echo "has rows";
else
echo "No rows";
exit();
I'm looking for a solution since two days. Please help me.
You can try
"execute VM_GETPRs_CAMPS ?,?"
Also if you have any PRINT statement on your stored procedure may cause some trouble
We would like to query our database and sample records based on a particular user's department.
Given the app design, the only way I know to do this is to filter the query by using dept's session variable.
The code below is not working.
Data gets returned if I comment out the session variable.
Nothing is getting retrieved with the session variable as filter.
Any ideas what I am doing wrong?
Is the session variable route the wrong approach?
Thanks in advance for help.
//Start your session
session_start();
// Connect to SQL Server database
include("../sqlConnect.php");
// Construct query
$loginName = $_GET['loginName'];
//Ok, let's grab user's dept from query
$sql = "SELECT ISNULL(dept,'NA') AS 'dept' FROM emp where ([userName]) = lower('$userName')"
$stmt = sqlsrv_query( $conn, $sql);
if( $stmt === false )
{
echo "Error in executing query.</br>";
die( print_r( sqlsrv_errors(), true));
}
$results = array();
while($row = sqlsrv_fetch_array($stmt,SQLSRV_FETCH_ASSOC)) {
array_push($results,$row);
$dept = $row['dept'];
}
//Store department in the session
$_SESSION['dept'] = $dept;
//Now extract records based on dept
$tsql ="SELECT
r.REQUESTID,
convert(char(10),r.[currDate],101),
r.[startedDBY],
e.[dept],
e.[WORKPHONE],
e.[EMAIL],
r.[DESCRIPTION],
r.[DETAILS],
r.[PROBLOCATION],
c.[COMMENTS]'
FROM EMPLOYEE e,REQUEST r,CUSTOMERCALL c
WHERE LOGINNAME='$loginName'
AND c.REQUESTID=r.requestid
AND e.dept = '" . $_SESSION['dept'] . "'
";
$stmt = sqlsrv_query( $conn, $tsql);
if( $stmt === false )
{
echo "Error in executing query.</br>";
die( print_r( sqlsrv_errors(), true));
}
$results = array();
while($row = sqlsrv_fetch_array($stmt,SQLSRV_FETCH_ASSOC)) {
array_push($results,$row);
}
echo json_encode($results);
// Free connection resources
sqlsrv_free_stmt( $stmt);
sqlsrv_close( $conn);