0

I have an application right now that uses SQL Server 2017. When I am executing a stored procedure on SSMS, it shows all of the data. But then, on PHP, when I am executing it, it only shows the first data that comes up.

This is the command:

EXEC dbo.SP_Get_Books_Archive;

SQL Server Results

The stored procedure I am using:

CREATE PROCEDURE SP_Get_Books_Archive
AS
BEGIN
SELECT 
   Book_ISBN as ISBN, 
   Book_Name, Book_Author, 
   Category_Name, 
   Book_Status, 
   Book_Copies_Current, 
   Book_Copies_Actual 
FROM Book 
LEFT JOIN Book_Category ON (Book.Book_Category_ID = Book_Category.Category_ID)
END

PHP Code I am using:

<?php
$connection = sqlsrv_connect($server, $connectionInfo);
$query = "EXEC SP_Get_Books_Archive";
$statement = sqlsrv_prepare($connection, $query);
$result = sqlsrv_execute($statement);
$row = sqlsrv_fetch_array($statement);
$rowCount = 1;

var_dump($row);
if (count($row) > 0) {
    foreach ($row as $key => $value) {
        if ($key == 'ISBN') {
            echo "<tr>";
            echo "<td>" . $value . "</td>";
        } else if ($key == 'Book_Name') {
            echo "<td>" . $value . "</td>";
        } else if ($key == 'Book_Author') {
            echo "<td>" . $value . "</td>";
        } else if ($key == 'Category_Name') {
            echo "<td>" . $value . "</td>";
        } else if ($key == 'Book_Status') {
            echo "<td>" . $value . "</td>";
        } else if ($key == 'Book_Copies_Current') {
            echo "<td>" . $value . "</td>";
        } else if ($key == 'Book_Copies_Actual') {
            echo "<td>" . $value . "</td>";
        }
        if ($rowCount == count($row)) {
            echo "</tr>";
        }
        $rowCount++;
    }
}

(note: this is enclosed on a <?php include(get_books.php) ?> code located on another php file. tried not separating it but no avail)

To which the result only says: Second Image PHP Results

I also tried executing the full SELECT... script on the $query but same results. Is there something wrong with my PHP code or is it a SQL Server error I have missed?

This is somewhat similar to this post but I don't think the answer there is my issue.

2
  • As per the question guide, please do not post images of code, data, error messages, etc. - copy or type the text into the question. Please reserve the use of images for diagrams or demonstrating rendering bugs, things that are impossible to describe accurately via text. Commented Mar 2, 2022 at 9:18
  • 1
    FYI the prefix sp_ is reserved, by Microsoft, for Special / System Procedures. It should not be used for User Procedures. Doing so comes with a performance cost and the risk of your Procedure simply not working one day after an update/upgrade. Either use a different prefix or (possibly better) no prefix at all. Is the sp_ prefix still a no-no? Commented Mar 2, 2022 at 9:20

1 Answer 1

1

You need to fetch all data:

while ($row = sqlsrv_fetch_array($statement, SQLSRV_FETCH_ASSOC)) {
   ...
}

As an additional note, you need to check the result from each sqlsrv_ function call. An example, based on your code:

<?php
// Connection
$connection = sqlsrv_connect($server, $connectionInfo);
if ($connection === false ) {
    echo "Error (sqlsrv_connect): ".print_r(sqlsrv_errors(), true);
    exit;
}

// Statement
$query = "EXEC SP_Get_Books_Archive";
$statement = sqlsrv_prepare($connection, $query);
if ($statement === false) {
    echo "Error (sqlsrv_prepare): ".print_r(sqlsrv_errors(), true);
    exit;
}   

// Execution
$result = sqlsrv_execute($statement);
if ($result === false) {
    echo "Error (sqlsrv_execute): ".print_r(sqlsrv_errors(), true);
    exit;
}   

// Fetch data
$rowCount = 0;
while ($row = sqlsrv_fetch_array($statement, SQLSRV_FETCH_ASSOC)) {
    $rowCount++;
    echo "<tr>";
    foreach ($row as $key => $value) {
        if ($key == 'ISBN') {
            echo "<td>" . $value . "</td>";
        } else if ($key == 'Book_Name') {
            echo "<td>" . $value . "</td>";
        } else if ($key == 'Book_Author') {
            echo "<td>" . $value . "</td>";
        } else if ($key == 'Category_Name') {
            echo "<td>" . $value . "</td>";
        } else if ($key == 'Book_Status') {
            echo "<td>" . $value . "</td>";
        } else if ($key == 'Book_Copies_Current') {
            echo "<td>" . $value . "</td>";
        } else if ($key == 'Book_Copies_Actual') {
            echo "<td>" . $value . "</td>";
        }
    }
    echo "</tr>";
}

// End
sqlsrv_free_stmt($statement);
sqlsrv_close($connection);
?>
Sign up to request clarification or add additional context in comments.

1 Comment

sqlsrv_free_stmt and sqlsrv_close should be in a finally and the rest of the code in the try

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.