SSS
SSS

Reputation: 53

PHP to report status of MSSQL query table select into

I have a button:

<a href="#" onclick="loadPage('admin_manualsend.php?refresh=true'); return false;">
    <i class="fa fa-refresh"></i> 1. Refresh Now
</a>

and on click it loads the same page it is one with refresh=true so now when the page loads it triggers this:

if(isset($_GET["refresh"]) && $_GET["refresh"]=="true"){
    $res = db_query("begin try drop table [TmpTable] end try begin catch end catch");
    $res = db_query("SELECT * INTO [TmpTable] FROM [dbo].[ViewLive]");
}

It takes a while and I'd like it to report that the table drop was successful or not and also report the count of which record it is now transferring, and then when it finishes so something like this:

if(isset($_GET["refresh"]) && $_GET["refresh"]=="true"){
    $res = db_query("begin try drop table [TmpTable] end try begin catch end catch");
    Show modal, echo 'Drop done'; (or drop failed)
    WHILE ( $res = db_query("SELECT * INTO [TmpTable] FROM [dbo].[ViewLive]");
    echo 'Count of current record' 
    count of record +1
    )
    echo 'Update done'; 
    (user can then close modal)
}

How would I do this? I use: PHP, MSSQL, script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js">

Thank you

Update:

    function db_query($query) {
    global $conn;
    $res = sqlsrv_query($conn, $query);
    return $res;
}

function db_fetch_array($res) {
    $result = array();
    while( $array = sqlsrv_fetch_array($res) ) {
          $result[count($result)] = $array;    
    }  
    sqlsrv_free_stmt($res);
    return $result;
}

function db_fetch_row($res) {                   
    $array = sqlsrv_fetch_array($res);
    sqlsrv_free_stmt($res);
    return $array;
}

function db_fetch_one($res) {                   
    $array = sqlsrv_fetch_array($res);
    sqlsrv_free_stmt($res);
    return $array[0];

Upvotes: 0

Views: 76

Answers (1)

Icewine
Icewine

Reputation: 1851

I am not sure exactly what you are trying to do, but if you use ajax you can do your sql without leaving the page AND get a nice callback for success or error!

// put this in your document ready function for index.html
$("#submit").on("click", function() {
  $.ajax({
    type: "post",
    url: "process.php",
    data: "action=SomeActionHere",
    success: function(data) {
      alert("SUCCESS");
    },
    error: function(e) {
      alert("ERROR");
    }
  });
};
<!--index.html -->
<button id="submit">GOGO</button>

<!--process.php-->

<?php 
header("Content-type: application/json; charset=utf-8");
//connect to the server

$action = $_POST['action'];

if($action == "SomeActionHere"){

// do you sql stuff here

// when done sql
echo json_encode("SUCCESS"); // whatever data you want to send back

}

?>

Upvotes: 0

Related Questions