PHP Sqlserv Multiple Insert from Select

The problem I am facing is converting my PHP script from Mysqli to SqlServ, The code is to look for all the account numbers linked to a userid and then insert them into another data base to then be processed. The for loop i am using works on Mysqli and inserts the records correctly but when I change the script to work for SqlServer it only enters the first account number and rest account numbers are entered as NULL.

MySqli Code

{
if ($Account=="All Accounts")
{
    $sql = "SELECT * FROM accounts where userid='$userid'";
$result = mysqli_query($conn, $sql);

    
    $num_results = mysqli_num_rows($result);
    for ($i=0;$i<$num_results;$i++) {
        $row = mysqli_fetch_array($result);
        $name = $row['Account'];
        

     $sql = "INSERT INTO ReqDocs (Account, Date, Type, Number, Sent, Server) VALUES ('$name','$time', '2', '$email', 'No', '1' )";

SQlServ Code

if ($Account=="All Accounts")
{
    $sql = "SELECT * FROM accounts where userid='$userid'";
$params = array();
$options =  array( "Scrollable" => SQLSRV_CURSOR_KEYSET );
$stmt = sqlsrv_query( $conn, $sql , $params, $options );
$row_count = sqlsrv_num_rows( $stmt );
        for ($i=0;$i<$row_count;$i++)  {
         $row = sqlsrv_fetch_array($stmt);
        $name = $row['Account'];
        

for ($i=0;$i<$row_count;$i++)  {
     $row = sqlsrv_fetch_array($stmt);
    $name = $row['Account'];

     $sql = "INSERT INTO ReqDocs (Account, Date, Action, Type, Number, Sent, Server, userid)
VALUES (?, ?, ?, ?, ?, ?, ?, ?)";
        $params = array($name,$time, '1' , '1', $email, 'No', '1', $userid);

$stmt = sqlsrv_query( $conn, $sql, $params );

Honestly I feel i am missing something basic here bust cant seem to find it.



Read more here: https://stackoverflow.com/questions/67384115/php-sqlserv-multiple-insert-from-select

Content Attribution

This content was originally published by Paul Andrews at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: