Cancel a query when user leaves a web page

Cancel a query when user leaves a web page

am 18.11.2003 03:33:30 von Noel

This is a multi-part message in MIME format.

--Boundary_(ID_HmeUQXJ/q/MVS8FcWBpQCw)
Content-type: text/plain; format=flowed; charset=us-ascii
Content-transfer-encoding: 7BIT

Hi all,

I have a small problem. Is it possible to cancel a running query when a
person leave or pressed the stop button in my web page.

I've been thinking about mixing php and javascript. Is this the best way
to go about it?

basic out line of my code:
....stuff....
...stuff
$connection = pg_connect(host, dbname, user);

pg_send_query($connection, "SELECT * FROM blah");

// collect the results and display etc..
?>

The query can take some time.
And when the page is left the query is still running on postmaster.

Have tried
include "functions.php";
$connection = pg_connect(host, dbname, user);
?>
)>
echo $connection;
pg_send_query($connection, "SELECT * FROM blah");

// collect the results and display etc..
?>


The function, close_connection($connection), is defined in the include
file, which is the connection is busy, cancels the query and closes the
connection.

This gives the following error:

Resource id #1
Warning: pg_send_query(): 1 is not a valid PostgreSQL link resource in
/home/fauxn/public_html/singleAArepeats/test.php on line 11

Any suggestions welcome.

Many thanks

--
Noel Faux
Department of Biochemistry and Molecular Biology
Monash University
Clayton 3168
Victoria
Australia


--Boundary_(ID_HmeUQXJ/q/MVS8FcWBpQCw)
Content-type: text/html; charset=us-ascii
Content-transfer-encoding: 7BIT








Hi all,



I have a small problem. Is it possible to cancel a running query when a
person leave or pressed the stop button in my web page.



I've been thinking about mixing php and javascript. Is this the best
way to go about it?



basic out line of my code:

<html><head>....stuff....</head>

<body>...stuff

<?php

    $connection = pg_connect(host, dbname, user);



    pg_send_query($connection, "SELECT * FROM blah");



    // collect the results and display etc..

?>

</body></html>

The query can take some time.

And when the page is left the query is still running on postmaster.



Have tried

<?php

    include "functions.php";

    $connection = pg_connect(host, dbname, user);

?>

<html><body
onunload(<?close_connection($connection)?>)>

<?php

    echo $connection;

    pg_send_query($connection, "SELECT * FROM blah");



    // collect the results and display etc..

?>

</body></html>



The function, close_connection($connection), is defined in the include
file, which is the connection is busy, cancels the query and closes the
connection.



This gives the following error:



Resource id #1

Warning: pg_send_query(): 1 is not a valid PostgreSQL link
resource in /home/fauxn/public_html/singleAArepeats/test.php on
line 11



Any suggestions welcome.



Many thanks


-- 
Noel Faux
Department of Biochemistry and Molecular Biology
Monash University
Clayton 3168
Victoria
Australia




--Boundary_(ID_HmeUQXJ/q/MVS8FcWBpQCw)--

Re: Cancel a query when user leaves a web page

am 19.11.2003 03:05:35 von ljb

noel.faux@med.monash.edu.au wrote:
> I have a small problem. Is it possible to cancel a running query when a
> person leave or pressed the stop button in my web page.
> ...
> Have tried
> > include "functions.php";
> $connection = pg_connect(host, dbname, user);
> ?>
> )>
....

This will never work, as you found. The Javascript action is client-side
and PHP is server-side.

Try using register_shutdown_function() to register a function which will
call pg_cancel_query(). As long as PHP isn't ignoring user aborts (see
ignore_user_abort()), your query will be cancelled when the user hits STOP.
Another way is to ignore user aborts, and while waiting for the
asynchronous query to finish, keep calling connection_aborted() to see if
you are still connected to the user; if not you cancel the query and exit.

---------------------------(end of broadcast)---------------------------
TIP 1: subscribe and unsubscribe commands go to majordomo@postgresql.org

Re: Cancel a query when user leaves a web page

am 19.11.2003 18:44:08 von middink

This is a multi-part message in MIME format.

------=_NextPart_000_009D_01C3AF07.CA3727B0
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

)>

onunload above can't call function from PHP, but it called from javascript,=
you can create javascript function to call PHP function/file that you wann=
a PHP do=20

// close_connection.php
include('connection.php');
global $connection;

function close_connection($connection){
// close_connection here
// .....
// .....
// .....

}

close_connection($connection);
echo "";
?>

---------










----- Original Message -----=20
From: Noel=20
To: pgsql-php@postgresql.org=20
Sent: Tuesday, November 18, 2003 10:33 AM
Subject: [PHP] Cancel a query when user leaves a web page


Hi all,

I have a small problem. Is it possible to cancel a running query when a p=
erson leave or pressed the stop button in my web page.

I've been thinking about mixing php and javascript. Is this the best way =
to go about it?

basic out line of my code:
....stuff....
...stuff
$connection =3D pg_connect(host, dbname, user);

pg_send_query($connection, "SELECT * FROM blah");

// collect the results and display etc..
?>

The query can take some time.
And when the page is left the query is still running on postmaster.

Have tried
include "functions.php";
$connection =3D pg_connect(host, dbname, user);
?>
)>
echo $connection;
pg_send_query($connection, "SELECT * FROM blah");

// collect the results and display etc..
?>


The function, close_connection($connection), is defined in the include fi=
le, which is the connection is busy, cancels the query and closes the conne=
ction.

This gives the following error:

Resource id #1
Warning: pg_send_query(): 1 is not a valid PostgreSQL link resource in /h=
ome/fauxn/public_html/singleAArepeats/test.php on line 11

Any suggestions welcome.

Many thanks

--=20
Noel Faux
Department of Biochemistry and Molecular Biology
Monash University
Clayton 3168
Victoria
Australia

------=_NextPart_000_009D_01C3AF07.CA3727B0
Content-Type: text/html;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable









size=3D1>

<body=20
onunload(<?close_connection($connection)?>)>


onunload above can't call fu=
nction from=20
PHP, but it called from javascript, you can create javascript fun=
ction=20
to call PHP function/file that you wanna PHP do 

=20
face=3D"Courier New" color=3D#ff0000 size=3D1>

<?php
=20
face=3D"Courier New" color=3D#ff8000 size=3D1>// close_connection.php
FONT> face=3D"Courier New" size=3D1>
color=3D#000000>include(
=20
color=3D#ff0000 size=3D1>'connection.php'
ew"=20
color=3D#000000 size=3D1>);
global
ew"=20
color=3D#580000 size=3D1>$connection
=3D#000000=20
size=3D1>;

function close_connection(
rier New"=20
color=3D#580000 size=3D1>$connection
=3D#000000=20
size=3D1>){
1>//=20
close_connection here
size=3D1>
=
//=20
......

face=3D"Courier New" color=3D#ff8000 size=3D1>// ..... face=3D"Courier New" size=3D1>
r=3D#ff8000=20
size=3D1>// .....

NT=20
color=3D#000000>}

close_connection(
er New"=20
color=3D#580000 size=3D1>$connection
=3D#000000=20
size=3D1>);
echo
0000=20
size=3D1>"<SCRIPT>window.close();</SCRIPT>"
face=3D"Courier New" color=3D#000000 size=3D1>;
rier New"=20
color=3D#ff0000 size=3D1>?>


=20
face=3D"Courier New" color=3D#0000ff size=3D1> 00000=20
size=3D2>---------


<html T> face=3D"Courier New" color=3D#0000ff size=3D1>>
Courier New"=20
size=3D1>
<=
;
face=3D"Courier New" color=3D#800000 size=3D1>SCRIPT urier New"=20
color=3D#008080 size=3D1>
=20
size=3D1>LANGUAGE
size=3D1>=3D"JavaScript">
=
face=3D"Courier New" color=3D#585858 size=3D1><!-- ourier New"=20
color=3D#008080 size=3D1>
=20
size=3D1>Begin

=20
face=3D"Courier New" color=3D#585858 size=3D1>function Courier New"=20
color=3D#008080 size=3D1>
=20
size=3D1>leave()
=
face=3D"Courier New" color=3D#585858 size=3D1>{ New"=20
size=3D1>
size=3D1>window.open('close_connection.php','','toolbar=3Dno ,menubar=3Dno,l=
ocation=3Dno,height=3D10,width=3D10');
face=3D"Courier New" size=3D1>
=3D#585858=20
size=3D1>}

face=3D"Courier New" color=3D#585858 size=3D1>// r New"=20
color=3D#008080 size=3D1>
=20
size=3D1>End
ONT> face=3D"Courier New" color=3D#585858 size=3D1>--> urier New"=20
size=3D1>
size=3D1></ size=3D1>SCRIPT size=3D1>>

T=20
face=3D"Courier New" color=3D#0000ff size=3D1>< ier New"=20
color=3D#800000 size=3D1>body
080 size=3D1>=20
onunload<=
FONT=20
face=3D"Courier New" color=3D#008080 size=3D1> New"=20
color=3D#0000ff size=3D1>=3D
80 size=3D1>=20
size=3D1>"leave()">

FONT> face=3D"Courier New" color=3D#0000ff size=3D1></
rier New"=20
color=3D#800000 size=3D1>body
0ff=20
size=3D1>>

face=3D"Courier New" color=3D#0000ff size=3D1></ rier New"=20
color=3D#800000 size=3D1>html
0ff=20
size=3D1>>


style=3D"PADDING-RIGHT: 0px; PADDING-LEFT: 5px; MARGIN-LEFT: 5px; BORDER-LE=
FT: #000000 2px solid; MARGIN-RIGHT: 0px">
----- Original Message -----

style=3D"BACKGROUND: #e4e4e4; FONT: 10pt arial; font-color: black">Fro=
m:
=20
href=3D"mailto:noel.faux@med.monash.edu.au">Noel
To: l.org=20
href=3D"mailto:pgsql-php@postgresql.org">pgsql-php@postgresq l.org
IV>
Sent: Tuesday, November 18, 2003 1=
0:33=20
AM

Subject: [PHP] Cancel a query when=
user=20
leaves a web page


Hi all,

I have a small problem. Is it possible to c=
ancel=20
a running query when a person leave or pressed the stop button in my web=
=20
page.

I've been thinking about mixing php and javascript. Is this =
the=20
best way to go about it?

basic out line of my=20
code:
<html><head>....stuff....</head>
<body&g=
t;...stuff
<?php
   =20
$connection =3D pg_connect(host, dbname, user);

   =
=20
pg_send_query($connection, "SELECT * FROM blah");

  &nbs=
p; //=20
collect the results and display=20
etc..
?>
</body></html>
The query can take some=
=20
time.
And when the page is left the query is still running on=20
postmaster.

Have tried
<?php
    include=
=20
"functions.php";
    $connection =3D pg_connect(host, d=
bname,=20
user);
?>
<html><body=20
onunload(<?close_connection($connection)?>)>
<?php
&nbs=
p;  =20
echo $connection;
    pg_send_query($connection, "SELEC=
T *=20
FROM blah");

    // collect the results and display=
=20
etc..
?>
</body></html>

The function,=20
close_connection($connection), is defined in the include file, which is t=
he=20
connection is busy, cancels the query and closes the connection.

T=
his=20
gives the following error:

Resource id #1
Warning:=20
pg_send_query(): 1 is not a valid PostgreSQL link resource in=20
/home/fauxn/public_html/singleAArepeats/test.php on line=20
11

Any suggestions welcome.

Many thanks
RE class=3Dmoz-signature cols=3D"72">--=20
Noel Faux
Department of Biochemistry and Molecular Biology
Monash University
Clayton 3168
Victoria
Australia


------=_NextPart_000_009D_01C3AF07.CA3727B0--

Re: Cancel a query when user leaves a web page

am 16.12.2003 08:58:17 von Noel

This is a multi-part message in MIME format.

--Boundary_(ID_guCDxg5vhcnlCQv3p5oAOw)
Content-type: text/plain; format=flowed; charset=us-ascii
Content-transfer-encoding: 7BIT

Hi all,
Sorry for the delayed response, I've added both Hayat's and ljb220's
suggestions.

I've tried both, but still having problems.
Code:
//----------------------
test.php



global $connection;
$connection = pg_connect("host=localhost dbname=test user=test");
echo $connection." connection
";
echo pg_connection_status($connection)." status
";
pg_send_query($connection, "SELECT COUNT(id) FROM test"); //
expected to take ~2min enough time to press stop
echo pg_connection_busy($connection)." busy
";
$res = pg_get_result($connection);
echo pg_num_rows($res)." # rows
";
echo $GLOBALS["connection"]." GLOBALS
";
?>


//--------------------------------------
close.php
echo "closing connection
";
$f = fopen("/home/xxx/log","w"); // log file to trace the code etc...
fwrite($f,"new page\n");
fclose($f);
close_con();
echo "closed";
echo "";
?>

//--------------------------------
functions.php
/* when the page is exited, if the connection is not null, cancel and
sql statements and
close the database connection */
function close_con() {
$f = fopen("/home/xxx/log","w"); // log file to trace the code
etc...
$connection = $GLOBALS["connection"];
fwrite($f,"Closing connection\n");
$status = pg_connection_status($connection);
fwrite($f, $status." status\n");
if($status == 0){// connected
fwrite($f, "connected\n");
if(pg_connection_busy($connection)) {// busy so cancel last
query
fwrite($f, "connection busy\n");
pg_cancel_query($connection);
fwrite($f, "query killed\n");
}
// close connection
pg_close($connection);
fwrite($f, "connection closed");
}
else {
fwrite($f, "not connected\n");
}
fclose($f);
}
?>

With this set up, the user enters test.php. Stops before the query
finishes. It should call leave(). However, this dose not happen. I
believe it is because the script has not finished and passed the page
text to the browser (pls correct me if I'm wrong), until php has
finished processing it.

I've tried using register_shutdown_function() as:
test.php


global $connection;
$connection = pg_connect("host=localhost dbname=test user=test");
register_shutdown_function("close_con");
echo $connection." connection
";
echo pg_connection_status($connection)." status
";
pg_send_query($connection, "SELECT COUNT(id) FROM test");
echo pg_connection_busy($connection)." busy
";
$res = pg_get_result($connection);
echo pg_num_rows($res)." # rows
";
echo $GLOBALS["connection"]." GLOBALS
";
?>


The functions.php as above.

This also dose not work as, " The registered shutdown functions are
called after the request has been completed" (from
http://au2.php.net/manual/en/function.register-shutdown-func tion.php ).
I'm interpreting that as, any functions in the list will not be
called until the pg_send_query() or the pg_get_result(), not sure which,
returns. Then close_con() from functions.php is called via
register_shutdown_function(). Thus I'm unable to cancel the query :(

Any comments, suggestions or work arounds would be greatly appreciated.

I'm using PHP 4.2.3

Cheers
Noel

//---------------------------------------------------------- -------------------
Muhyiddin A.M Hayat wrote:

> )>
>
> onunload above can't call function from PHP, but it called from
> javascript, you can create javascript function to call PHP
> function/file that you wanna PHP do
>
> > // close_connection.php
> include('connection.php');
> global $connection;
>
> function close_connection($connection){
> // close_connection here
> // .....
> // .....
> // .....
>
> }
>
> close_connection($connection);
> echo "";
> ?>
>
> ---------
>
>
>
>
>
>
>
>
>
>
//----------------------------------------
ljb wrote:

noel.faux@med.monash.edu.au wrote:

>I have a small problem. Is it possible to cancel a running query when a
>person leave or pressed the stop button in my web page.
>...
>Have tried
> > include "functions.php";
> $connection = pg_connect(host, dbname, user);
>?>
>)>
>
>
> ...
>
> This will never work, as you found. The Javascript action is client-side
> and PHP is server-side.
>
> Try using register_shutdown_function() to register a function which will
> call pg_cancel_query(). As long as PHP isn't ignoring user aborts (see
> ignore_user_abort()), your query will be cancelled when the user hits STOP.
> Another way is to ignore user aborts, and while waiting for the
> asynchronous query to finish, keep calling connection_aborted() to see if
> you are still connected to the user; if not you cancel the query and exit.



--
Noel Faux
Department of Biochemistry and Molecular Biology
Monash University
Clayton 3168
Victoria
Australia


--Boundary_(ID_guCDxg5vhcnlCQv3p5oAOw)
Content-type: text/html; charset=us-ascii
Content-transfer-encoding: 7BIT








Hi all,

Sorry for the delayed response, I've added both Hayat's and ljb220's
suggestions.



I've tried both, but still having problems.

Code:

//----------------------

test.php

<html>

<script language="JavaScript">

<!-- Begin

function leave() {

   
window.open('close.php','','toolbar=no,menubar=no,location=n o,height=10,width=10','');

}

// end -->

</script>

<body onunload = 'leave()'>

<?php include "functions.php";

    global $connection;

    $connection = pg_connect("host=localhost dbname=test user=test");

    echo $connection." connection <br>";

    echo pg_connection_status($connection)." status<br>";

    pg_send_query($connection, "SELECT COUNT(id) FROM test"); //
expected to take ~2min enough time to press stop

    echo pg_connection_busy($connection)." busy<br>";

    $res = pg_get_result($connection);

    echo pg_num_rows($res)." # rows<br>";

    echo $GLOBALS["connection"]." GLOBALS<br>";

?>

</body></html>



//--------------------------------------

close.php

<?php include "functions.php";

    echo "closing connection<br>";

    $f = fopen("/home/xxx/log","w"); // log file to trace the code
etc...

    fwrite($f,"new page\n");

    fclose($f);

    close_con();

    echo "closed";

    echo "<script>window.close();</script>";

?>



//--------------------------------

functions.php

<?php

/* when the page is exited, if the connection is not null, cancel and
sql statements and

      close the database connection */

    function close_con() {

        $f = fopen("/home/xxx/log","w"); // log file to trace the code
etc...

        $connection = $GLOBALS["connection"];

        fwrite($f,"Closing connection\n");

        $status = pg_connection_status($connection);

        fwrite($f, $status." status\n");

        if($status == 0){// connected

            fwrite($f, "connected\n");

            if(pg_connection_busy($connection)) {// busy so cancel last
query

                fwrite($f, "connection busy\n");

                pg_cancel_query($connection);

                fwrite($f, "query killed\n");

            }

            // close connection

            pg_close($connection);

            fwrite($f, "connection closed");

        }

        else {

            fwrite($f, "not connected\n");

        }

        fclose($f);

    }

?>



With this set up, the user enters test.php. Stops before the query
finishes. It should call leave(). However, this dose not happen. I
believe it is because the script has not finished and passed the page
text to the browser (pls correct me if I'm wrong), until php has
finished processing it.



I've tried using register_shutdown_function() as:

test.php

<html>

<body>

<?php include "functions.php";

    global $connection;

    $connection = pg_connect("host=localhost dbname=test user=test");

    register_shutdown_function("close_con");

    echo $connection." connection <br>";

    echo pg_connection_status($connection)." status<br>";

    pg_send_query($connection, "SELECT COUNT(id) FROM test");

    echo pg_connection_busy($connection)." busy<br>";

    $res = pg_get_result($connection);

    echo pg_num_rows($res)." # rows<br>";

    echo $GLOBALS["connection"]." GLOBALS<br>";

?>

</body></html>



The functions.php as above.



This also dose not work as, " The registered shutdown functions are
called after the request has been completed" (from
).
I'm interpreting that as, any functions in the list will not be

called until the pg_send_query() or the pg_get_result(), not sure
which, returns. Then close_con() from functions.php is called via
register_shutdown_function(). Thus I'm unable to cancel the query :(



Any comments, suggestions or work arounds would be greatly appreciated.



I'm using PHP 4.2.3



Cheers

Noel



//---------------------------------------------------------- -------------------

Muhyiddin A.M Hayat wrote:

cite="mid00aa01c3aec5$3fb5df60$2b179fca@middinkcomp">





color="#ff0000" size="1">

color="#ff0000" size="1"><body
onunload(<?close_connection($connection)?>)>


color="#ff0000" size="1">onunload
above can't call function from PHP, but it called from javascript, you
can create javascript function to call PHP function/file that you wanna
PHP do 


size="1">

color="#ff0000" size="1"> size="1"><?php

//
close_connection.php


include(
face="Courier New" color="#ff0000" size="1">'connection.php'
face="Courier New" color="#000000" size="1">);

global
size="1">$connection size="1">;



function close_connection(
color="#580000" size="1">$connection color="#000000" size="1">){

//
close_connection here


// ..... face="Courier New" size="1">

// ..... face="Courier New" size="1">

// .....



}



close_connection(
size="1">$connection size="1">);

echo
size="1">"<SCRIPT>window.close();</SCRIPT>" face="Courier New" color="#000000" size="1">;

?>


color="#ff0000" size="1"> size="1">---------


color="#ff0000" size="1"> size="1"><html face="Courier New" color="#0000ff" size="1">>



< face="Courier New" color="#800000" size="1">SCRIPT
face="Courier New" color="#008080" size="1"> face="Courier New" color="#ff0000" size="1">LANGUAGE face="Courier New" color="#0000ff" size="1">="JavaScript"> face="Courier New" size="1">

<!-- face="Courier New" color="#008080" size="1"> face="Courier New" color="#585858" size="1">Begin face="Courier New" size="1">

function face="Courier New" color="#008080" size="1"> face="Courier New" color="#585858" size="1">leave() face="Courier New" color="#008080" size="1"> face="Courier New" color="#585858" size="1">{ face="Courier New" size="1">

window.open('close_connection.php','','toolbar=no,menubar=no ,location=no,height=10,width=10'); face="Courier New" size="1">

} face="Courier New" size="1">

// face="Courier New" color="#008080" size="1"> face="Courier New" color="#585858" size="1">End face="Courier New" color="#008080" size="1"> face="Courier New" color="#585858" size="1">--> face="Courier New" size="1">

</ face="Courier New" color="#800000" size="1">SCRIPT face="Courier New" color="#0000ff" size="1">>



< face="Courier New" color="#800000" size="1">body face="Courier New" color="#008080" size="1"> face="Courier New" color="#ff0000" size="1">onunload face="Courier New" color="#008080" size="1"> face="Courier New" color="#0000ff" size="1">= face="Courier New" color="#008080" size="1"> face="Courier New" color="#0000ff" size="1">"leave()">



</ face="Courier New" color="#800000" size="1">body face="Courier New" color="#0000ff" size="1">> face="Courier New" size="1">

</ face="Courier New" color="#800000" size="1">html face="Courier New" color="#0000ff" size="1">>





//----------------------------------------

ljb wrote:

 wrote:


I have a small problem. Is it possible to cancel a running query when a 
person leave or pressed the stop button in my web page.
....
Have tried
<?php
include "functions.php";
$connection = pg_connect(host, dbname, user);
?>
<html><body onunload(<?close_connection($connection)?>)>


> ...
>
> This will never work, as you found. The Javascript action is client-side
> and PHP is server-side.
>
> Try using register_shutdown_function() to register a function which will
> call pg_cancel_query(). As long as PHP isn't ignoring user aborts (see
> ignore_user_abort()), your query will be cancelled when the user hits STOP.
> Another way is to ignore user aborts, and while waiting for the
> asynchronous query to finish, keep calling connection_aborted() to see if
> you are still connected to the user; if not you cancel the query and exit.





-- 
Noel Faux
Department of Biochemistry and Molecular Biology
Monash University
Clayton 3168
Victoria
Australia




--Boundary_(ID_guCDxg5vhcnlCQv3p5oAOw)--