Cancel a query when user leaves a web page
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:
<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)
Content-type: text/html; charset=us-ascii
Content-transfer-encoding: 7BIT
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=ISO-8859-1">
<title></title>
</head>
<body text="#000000" bgcolor="#ffffff">
Hi all,<br>
<br>
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.<br>
<br>
I've been thinking about mixing php and javascript. Is this the best
way to go about it?<br>
<br>
basic out line of my code:<br>
<html><head>....stuff....</head><br>
<body>...stuff<br>
<?php<br>
$connection = pg_connect(host, dbname, user);<br>
<br>
pg_send_query($connection, "SELECT * FROM blah");<br>
<br>
// collect the results and display etc..<br>
?><br>
</body></html><br>
The query can take some time.<br>
And when the page is left the query is still running on postmaster.<br>
<br>
Have tried<br>
<?php <br>
include "functions.php";<br>
$connection = pg_connect(host, dbname, user);<br>
?><br>
<html><body
onunload(<?close_connection($connection)?>)><br>
<?php<br>
echo $connection;<br>
pg_send_query($connection, "SELECT * FROM blah");<br>
<br>
// collect the results and display etc..<br>
?><br>
</body></html><br>
<br>
The function, close_connection($connection), is defined in the include
file, which is the connection is busy, cancels the query and closes the
connection.<br>
<br>
This gives the following error:<br>
<br>
Resource id #1<br>
<b>Warning</b>: pg_send_query(): 1 is not a valid PostgreSQL link
resource in <b>/home/fauxn/public_html/singleAArepeats/test.php</b> on
line <b>11<br>
<br>
</b>Any suggestions welcome.<br>
<br>
Many thanks<br>
<b></b>
<pre class="moz-signature" cols="72">--
Noel Faux
Department of Biochemistry and Molecular Biology
Monash University
Clayton 3168
Victoria
Australia
</pre>
</body>
</html>
--Boundary_(ID_HmeUQXJ/q/MVS8FcWBpQCw)--
Re: Cancel a query when user leaves a web page
noel.faux [at] 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
> <?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.
---------------------------(end of broadcast)---------------------------
TIP 1: subscribe and unsubscribe commands go to majordomo [at] postgresql.org
Re: Cancel a query when user leaves a web page
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
<body onunload(<?close_connection($connection)?>)>
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
<?php
// close_connection.php
include('connection.php');
global $connection;
function close_connection($connection){
// close_connection here
// .....
// .....
// .....
}
close_connection($connection);
echo "<SCRIPT>window.close();</SCRIPT>";
?>
---------
<html>
<SCRIPT LANGUAGE=3D"JavaScript">
<!-- Begin
function leave() {
window.open('close_connection.php','','toolbar=3Dno,menubar= 3Dno,location=
=3Dno,height=3D10,width=3D10');
}
// End -->
</SCRIPT>
<body onunload =3D "leave()">
</body>
</html>
----- Original Message -----
From: Noel
To: pgsql-php [at] postgresql.org
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:
<html><head>....stuff....</head>
<body>...stuff
<?php
$connection =3D 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 =3D 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 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
--
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
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD><TITLE></TITLE>
<META http-equiv=3DContent-Type content=3Dtext/html;charset=3DISO-8859-1>
<META content=3D"MSHTML 6.00.2800.1170" name=3DGENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY text=3D#000000 bgColor=3D#ffffff>
<DIV>
<DIV><FONT face=3DArial size=3D2><FONT face=3D"Courier New" color=3D#ff0000=
size=3D1>
<P><FONT face=3DArial color=3D#000000 size=3D2><body
onunload(<?close_connection($connection)?>)></FONT></P>
<P><FONT face=3DArial color=3D#000000 size=3D2>onunload above can't call fu=
nction from
PHP, but it called from javascript, you can create javascript fun=
ction
to call PHP function/file that you wanna PHP do </P></FONT><FONT=
face=3D"Courier New" color=3D#ff0000 size=3D1>
<P><?php</FONT><FONT face=3D"Courier New" size=3D1><BR></FONT><I><FONT=
face=3D"Courier New" color=3D#ff8000 size=3D1>// close_connection.php</I></=
FONT><FONT
face=3D"Courier New" size=3D1><BR><FONT
color=3D#000000><B>include</B>(</FONT></FONT><B><FONT face=3D"Courier New"=
color=3D#ff0000 size=3D1>'connection.php'</B></FONT><FONT face=3D"Courier N=
ew"
color=3D#000000 size=3D1>);<BR><B>global</B> </FONT><FONT face=3D"Courier N=
ew"
color=3D#580000 size=3D1>$connection</FONT><FONT face=3D"Courier New" color=
=3D#000000
size=3D1>;<BR><BR><B>function</B> close_connection(</FONT><FONT face=3D"Cou=
rier New"
color=3D#580000 size=3D1>$connection</FONT><FONT face=3D"Courier New" color=
=3D#000000
size=3D1>){<BR></FONT><I><FONT face=3D"Courier New" color=3D#ff8000 size=3D=
1>//
close_connection here</I></FONT><FONT face=3D"Courier New"
size=3D1><BR></FONT><I><FONT face=3D"Courier New" color=3D#ff8000 size=3D1>=
//
......</I></FONT><FONT face=3D"Courier New" size=3D1><BR></FONT><I><FONT
face=3D"Courier New" color=3D#ff8000 size=3D1>// .....</I></FONT><FONT
face=3D"Courier New" size=3D1><BR></FONT><I><FONT face=3D"Courier New" colo=
r=3D#ff8000
size=3D1>// .....<BR></I></FONT><FONT face=3D"Courier New" size=3D1><BR><FO=
NT
color=3D#000000>}<BR><BR>close_connection(</FONT></FONT><FONT face=3D"Couri=
er New"
color=3D#580000 size=3D1>$connection</FONT><FONT face=3D"Courier New" color=
=3D#000000
size=3D1>);<BR><B>echo</B> </FONT><B><FONT face=3D"Courier New" color=3D#ff=
0000
size=3D1>"<SCRIPT>window.close();</SCRIPT>"</B></FONT><FONT
face=3D"Courier New" color=3D#000000 size=3D1>;<BR></FONT><FONT face=3D"Cou=
rier New"
color=3D#ff0000 size=3D1>?></P>
<P></FONT></FONT><FONT face=3D"Courier New" color=3D#ff0000 size=3D1><FONT=
face=3D"Courier New" color=3D#0000ff size=3D1><FONT face=3DArial color=3D#0=
00000
size=3D2>---------</FONT></P>
<P><</FONT><FONT face=3D"Courier New" color=3D#800000 size=3D1>html</FON=
T><FONT
face=3D"Courier New" color=3D#0000ff size=3D1>><BR></FONT><FONT face=3D"=
Courier New"
size=3D1><BR></FONT><FONT face=3D"Courier New" color=3D#0000ff size=3D1><=
;</FONT><FONT
face=3D"Courier New" color=3D#800000 size=3D1>SCRIPT</FONT><FONT face=3D"Co=
urier New"
color=3D#008080 size=3D1> </FONT><FONT face=3D"Courier New" color=3D#ff0000=
size=3D1>LANGUAGE</FONT><FONT face=3D"Courier New" color=3D#0000ff
size=3D1>=3D"JavaScript"></FONT><FONT face=3D"Courier New" size=3D1><BR>=
</FONT><FONT
face=3D"Courier New" color=3D#585858 size=3D1><!--</FONT><FONT face=3D"C=
ourier New"
color=3D#008080 size=3D1> </FONT><FONT face=3D"Courier New" color=3D#585858=
size=3D1>Begin</FONT><FONT face=3D"Courier New" size=3D1><BR></FONT><FONT=
face=3D"Courier New" color=3D#585858 size=3D1>function</FONT><FONT face=3D"=
Courier New"
color=3D#008080 size=3D1> </FONT><FONT face=3D"Courier New" color=3D#585858=
size=3D1>leave()</FONT><FONT face=3D"Courier New" color=3D#008080 size=3D1>=
</FONT><FONT
face=3D"Courier New" color=3D#585858 size=3D1>{</FONT><FONT face=3D"Courier=
New"
size=3D1><BR></FONT><FONT face=3D"Courier New" color=3D#585858
size=3D1>window.open('close_connection.php','','toolbar=3Dno ,menubar=3Dno,l=
ocation=3Dno,height=3D10,width=3D10');</FONT><FONT
face=3D"Courier New" size=3D1><BR></FONT><FONT face=3D"Courier New" color=
=3D#585858
size=3D1>}</FONT><FONT face=3D"Courier New" size=3D1><BR></FONT><FONT
face=3D"Courier New" color=3D#585858 size=3D1>//</FONT><FONT face=3D"Courie=
r New"
color=3D#008080 size=3D1> </FONT><FONT face=3D"Courier New" color=3D#585858=
size=3D1>End</FONT><FONT face=3D"Courier New" color=3D#008080 size=3D1> </F=
ONT><FONT
face=3D"Courier New" color=3D#585858 size=3D1>--></FONT><FONT face=3D"Co=
urier New"
size=3D1><BR></FONT><FONT face=3D"Courier New" color=3D#0000ff
size=3D1></</FONT><FONT face=3D"Courier New" color=3D#800000
size=3D1>SCRIPT</FONT><FONT face=3D"Courier New" color=3D#0000ff
size=3D1>><BR></FONT><FONT face=3D"Courier New" size=3D1><BR></FONT><FON=
T
face=3D"Courier New" color=3D#0000ff size=3D1><</FONT><FONT face=3D"Cour=
ier New"
color=3D#800000 size=3D1>body</FONT><FONT face=3D"Courier New" color=3D#008=
080 size=3D1>
</FONT><FONT face=3D"Courier New" color=3D#ff0000 size=3D1>onunload</FONT><=
FONT
face=3D"Courier New" color=3D#008080 size=3D1> </FONT><FONT face=3D"Courier=
New"
color=3D#0000ff size=3D1>=3D</FONT><FONT face=3D"Courier New" color=3D#0080=
80 size=3D1>
</FONT><FONT face=3D"Courier New" color=3D#0000ff
size=3D1>"leave()"><BR></FONT><FONT face=3D"Courier New" size=3D1><BR></=
FONT><FONT
face=3D"Courier New" color=3D#0000ff size=3D1></</FONT><FONT face=3D"Cou=
rier New"
color=3D#800000 size=3D1>body</FONT><FONT face=3D"Courier New" color=3D#000=
0ff
size=3D1>></FONT><FONT face=3D"Courier New" size=3D1><BR></FONT><FONT
face=3D"Courier New" color=3D#0000ff size=3D1></</FONT><FONT face=3D"Cou=
rier New"
color=3D#800000 size=3D1>html</FONT><FONT face=3D"Courier New" color=3D#000=
0ff
size=3D1>></P></FONT></FONT></FONT></DIV></DIV>
<BLOCKQUOTE dir=3Dltr
style=3D"PADDING-RIGHT: 0px; PADDING-LEFT: 5px; MARGIN-LEFT: 5px; BORDER-LE=
FT: #000000 2px solid; MARGIN-RIGHT: 0px">
<DIV style=3D"FONT: 10pt arial">----- Original Message ----- </DIV>
<DIV
style=3D"BACKGROUND: #e4e4e4; FONT: 10pt arial; font-color: black"><B>Fro=
m:</B>
<A title=3Dnoel.faux [at] med.monash.edu.au
href=3D"mailto:noel.faux [at] med.monash.edu.au">Noel</A> </DIV>
<DIV style=3D"FONT: 10pt arial"><B>To:</B> <A title=3Dpgsql-php [at] postgresq=
l.org
href=3D"mailto:pgsql-php [at] postgresql.org">pgsql-php [at] postgresq l.org</A> </D=
IV>
<DIV style=3D"FONT: 10pt arial"><B>Sent:</B> Tuesday, November 18, 2003 1=
0:33
AM</DIV>
<DIV style=3D"FONT: 10pt arial"><B>Subject:</B> [PHP] Cancel a query when=
user
leaves a web page</DIV>
<DIV><BR></DIV>Hi all,<BR><BR>I have a small problem. Is it possible to c=
ancel
a running query when a person leave or pressed the stop button in my web=
page.<BR><BR>I've been thinking about mixing php and javascript. Is this =
the
best way to go about it?<BR><BR>basic out line of my
code:<BR><html><head>....stuff....</head><BR><body&g=
t;...stuff<BR><?php<BR>
$connection =3D pg_connect(host, dbname, user);<BR><BR> =
pg_send_query($connection, "SELECT * FROM blah");<BR><BR> &nbs=
p; //
collect the results and display
etc..<BR>?><BR></body></html><BR>The query can take some=
time.<BR>And when the page is left the query is still running on
postmaster.<BR><BR>Have tried<BR><?php <BR> include=
"functions.php";<BR> $connection =3D pg_connect(host, d=
bname,
user);<BR>?><BR><html><body
onunload(<?close_connection($connection)?>)><BR><?php<BR>&nbs=
p;
echo $connection;<BR> pg_send_query($connection, "SELEC=
T *
FROM blah");<BR><BR> // collect the results and display=
etc..<BR>?><BR></body></html><BR><BR>The function,
close_connection($connection), is defined in the include file, which is t=
he
connection is busy, cancels the query and closes the connection.<BR><BR>T=
his
gives the following error:<BR><BR>Resource id #1<BR><B>Warning</B>:
pg_send_query(): 1 is not a valid PostgreSQL link resource in
<B>/home/fauxn/public_html/singleAArepeats/test.php</B> on line
<B>11<BR><BR></B>Any suggestions welcome.<BR><BR>Many thanks<BR><B></B><P=
RE class=3Dmoz-signature cols=3D"72">--
Noel Faux
Department of Biochemistry and Molecular Biology
Monash University
Clayton 3168
Victoria
Australia
</PRE></BLOCKQUOTE></BODY></HTML>
------=_NextPart_000_009D_01C3AF07.CA3727B0--
Re: Cancel a query when user leaves a web page
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
<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
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:
> <body onunload(<?close_connection($connection)?>)>
>
> 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
>
> <?php
> // close_connection.php
> include('connection.php');
> global $connection;
>
> function close_connection($connection){
> // close_connection here
> // .....
> // .....
> // .....
>
> }
>
> close_connection($connection);
> echo "<SCRIPT>window.close();</SCRIPT>";
> ?>
>
> ---------
>
> <html>
>
> <SCRIPT LANGUAGE="JavaScript">
> <!-- Begin
> function leave() {
> window.open('close_connection.php','','toolbar=no,menubar=no ,location=no,height=10,width=10');
> }
> // End -->
> </SCRIPT>
>
> <body onunload = "leave()">
>
> </body>
> </html>
>
//----------------------------------------
ljb wrote:
noel.faux [at] 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
><?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)
Content-type: text/html; charset=us-ascii
Content-transfer-encoding: 7BIT
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=ISO-8859-1">
<title></title>
</head>
<body text="#000000" bgcolor="#ffffff">
Hi all,<br>
Sorry for the delayed response, I've added both Hayat's and ljb220's
suggestions.<br>
<br>
I've tried both, but still having problems.<br>
Code:<br>
//----------------------<br>
test.php<br>
<html><br>
<script language="JavaScript"><br>
<!-- Begin<br>
function leave() {<br>
window.open('close.php','','toolbar=no,menubar=no,location=n o,height=10,width=10','');<br>
}<br>
// end --><br>
</script><br>
<body onunload = 'leave()'><br>
<?php include "functions.php";<br>
global $connection;<br>
$connection = pg_connect("host=localhost dbname=test user=test");<br>
echo $connection." connection <br>";<br>
echo pg_connection_status($connection)." status<br>";<br>
pg_send_query($connection, "SELECT COUNT(id) FROM test"); //
expected to take ~2min enough time to press stop<br>
echo pg_connection_busy($connection)." busy<br>";<br>
$res = pg_get_result($connection);<br>
echo pg_num_rows($res)." # rows<br>";<br>
echo $GLOBALS["connection"]." GLOBALS<br>";<br>
?><br>
</body></html><br>
<br>
//--------------------------------------<br>
close.php<br>
<?php include "functions.php";<br>
echo "closing connection<br>";<br>
$f = fopen("/home/xxx/log","w"); // log file to trace the code
etc...<br>
fwrite($f,"new page\n");<br>
fclose($f);<br>
close_con();<br>
echo "closed";<br>
echo "<script>window.close();</script>";<br>
?><br>
<br>
//--------------------------------<br>
functions.php<br>
<?php<br>
/* when the page is exited, if the connection is not null, cancel and
sql statements and <br>
close the database connection */<br>
function close_con() {<br>
$f = fopen("/home/xxx/log","w"); // log file to trace the code
etc...<br>
$connection = $GLOBALS["connection"];<br>
fwrite($f,"Closing connection\n");<br>
$status = pg_connection_status($connection);<br>
fwrite($f, $status." status\n");<br>
if($status == 0){// connected<br>
fwrite($f, "connected\n");<br>
if(pg_connection_busy($connection)) {// busy so cancel last
query<br>
fwrite($f, "connection busy\n");<br>
pg_cancel_query($connection);<br>
fwrite($f, "query killed\n");<br>
}<br>
// close connection<br>
pg_close($connection);<br>
fwrite($f, "connection closed");<br>
}<br>
else {<br>
fwrite($f, "not connected\n");<br>
}<br>
fclose($f);<br>
}<br>
?><br>
<br>
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.<br>
<br>
I've tried using register_shutdown_function() as:<br>
test.php<br>
<html><br>
<body><br>
<?php include "functions.php";<br>
global $connection;<br>
$connection = pg_connect("host=localhost dbname=test user=test");<br>
register_shutdown_function("close_con");<br>
echo $connection." connection <br>";<br>
echo pg_connection_status($connection)." status<br>";<br>
pg_send_query($connection, "SELECT COUNT(id) FROM test");<br>
echo pg_connection_busy($connection)." busy<br>";<br>
$res = pg_get_result($connection);<br>
echo pg_num_rows($res)." # rows<br>";<br>
echo $GLOBALS["connection"]." GLOBALS<br>";<br>
?><br>
</body></html><br>
<br>
The functions.php as above.<br>
<br>
This also dose not work as, " The registered shutdown functions are
called after the request has been completed" (from
<a class="moz-txt-link-freetext" href="http://au2.php.net/manual/en/function.register-shutdown-function.php"> http://au2.php.net/manual/en/function.register-shutdown-func tion.php</a> ).
I'm interpreting that as, any functions in the list will not be <br>
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 :(<br>
<br>
Any comments, suggestions or work arounds would be greatly appreciated.<br>
<br>
I'm using PHP 4.2.3<br>
<br>
Cheers<br>
Noel<br>
<br>
//---------------------------------------------------------- -------------------<br>
Muhyiddin A.M Hayat wrote:<br>
<blockquote type="cite"
cite="mid00aa01c3aec5$3fb5df60$2b179fca [at] middinkcomp">
<title></title>
<meta http-equiv="Content-Type" content="text/html;charset=ISO-8859-1">
<meta content="MSHTML 6.00.2800.1170" name="GENERATOR">
<style></style>
<div>
<div><font face="Arial" size="2"><font face="Courier New"
color="#ff0000" size="1"></font></font>
<p><font face="Arial" size="2"><font face="Courier New"
color="#ff0000" size="1"><font face="Arial" color="#000000" size="2"><body
onunload(<?close_connection($connection)?>)></font></font></font></p>
<p><font face="Arial" size="2"><font face="Courier New"
color="#ff0000" size="1"><font face="Arial" color="#000000" size="2">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 </font></font></font></p>
<font face="Arial" size="2"><font face="Courier New" color="#ff0000"
size="1"><font face="Courier New" color="#ff0000" size="1"></font></font></font>
<p><font face="Arial" size="2"><font face="Courier New"
color="#ff0000" size="1"><font face="Courier New" color="#ff0000"
size="1"><?php</font><font face="Courier New" size="1"><br>
</font><i><font face="Courier New" color="#ff8000" size="1">//
close_connection.php</font></i><font face="Courier New" size="1"><br>
<font color="#000000"><b>include</b>(</font></font><b><font
face="Courier New" color="#ff0000" size="1">'connection.php'</font></b><font
face="Courier New" color="#000000" size="1">);<br>
<b>global</b> </font><font face="Courier New" color="#580000"
size="1">$connection</font><font face="Courier New" color="#000000"
size="1">;<br>
<br>
<b>function</b> close_connection(</font><font face="Courier New"
color="#580000" size="1">$connection</font><font face="Courier New"
color="#000000" size="1">){<br>
</font><i><font face="Courier New" color="#ff8000" size="1">//
close_connection here</font></i><font face="Courier New" size="1"><br>
</font><i><font face="Courier New" color="#ff8000" size="1">// .....</font></i><font
face="Courier New" size="1"><br>
</font><i><font face="Courier New" color="#ff8000" size="1">// .....</font></i><font
face="Courier New" size="1"><br>
</font><i><font face="Courier New" color="#ff8000" size="1">// .....<br>
</font></i><font face="Courier New" size="1"><br>
<font color="#000000">}<br>
<br>
close_connection(</font></font><font face="Courier New" color="#580000"
size="1">$connection</font><font face="Courier New" color="#000000"
size="1">);<br>
<b>echo</b> </font><b><font face="Courier New" color="#ff0000"
size="1">"<SCRIPT>window.close();</SCRIPT>"</font></b><font
face="Courier New" color="#000000" size="1">;<br>
</font><font face="Courier New" color="#ff0000" size="1">?></font></font></font></p>
<p><font face="Arial" size="2"><font face="Courier New"
color="#ff0000" size="1"><font face="Courier New" color="#0000ff"
size="1"><font face="Arial" color="#000000" size="2">---------</font></font></font></font></p>
<p><font face="Arial" size="2"><font face="Courier New"
color="#ff0000" size="1"><font face="Courier New" color="#0000ff"
size="1"><</font><font face="Courier New" color="#800000" size="1">html</font><font
face="Courier New" color="#0000ff" size="1">><br>
</font><font face="Courier New" size="1"><br>
</font><font face="Courier New" color="#0000ff" size="1"><</font><font
face="Courier New" color="#800000" size="1">SCRIPT</font><font
face="Courier New" color="#008080" size="1"> </font><font
face="Courier New" color="#ff0000" size="1">LANGUAGE</font><font
face="Courier New" color="#0000ff" size="1">="JavaScript"></font><font
face="Courier New" size="1"><br>
</font><font face="Courier New" color="#585858" size="1"><!--</font><font
face="Courier New" color="#008080" size="1"> </font><font
face="Courier New" color="#585858" size="1">Begin</font><font
face="Courier New" size="1"><br>
</font><font face="Courier New" color="#585858" size="1">function</font><font
face="Courier New" color="#008080" size="1"> </font><font
face="Courier New" color="#585858" size="1">leave()</font><font
face="Courier New" color="#008080" size="1"> </font><font
face="Courier New" color="#585858" size="1">{</font><font
face="Courier New" size="1"><br>
</font><font face="Courier New" color="#585858" size="1"> window.open('close_connection.php','','toolbar=no,menubar=no ,location=no,height=10,width=10');</font><font
face="Courier New" size="1"><br>
</font><font face="Courier New" color="#585858" size="1">}</font><font
face="Courier New" size="1"><br>
</font><font face="Courier New" color="#585858" size="1">//</font><font
face="Courier New" color="#008080" size="1"> </font><font
face="Courier New" color="#585858" size="1">End</font><font
face="Courier New" color="#008080" size="1"> </font><font
face="Courier New" color="#585858" size="1">--></font><font
face="Courier New" size="1"><br>
</font><font face="Courier New" color="#0000ff" size="1"></</font><font
face="Courier New" color="#800000" size="1">SCRIPT</font><font
face="Courier New" color="#0000ff" size="1">><br>
</font><font face="Courier New" size="1"><br>
</font><font face="Courier New" color="#0000ff" size="1"><</font><font
face="Courier New" color="#800000" size="1">body</font><font
face="Courier New" color="#008080" size="1"> </font><font
face="Courier New" color="#ff0000" size="1">onunload</font><font
face="Courier New" color="#008080" size="1"> </font><font
face="Courier New" color="#0000ff" size="1">=</font><font
face="Courier New" color="#008080" size="1"> </font><font
face="Courier New" color="#0000ff" size="1">"leave()"><br>
</font><font face="Courier New" size="1"><br>
</font><font face="Courier New" color="#0000ff" size="1"></</font><font
face="Courier New" color="#800000" size="1">body</font><font
face="Courier New" color="#0000ff" size="1">></font><font
face="Courier New" size="1"><br>
</font><font face="Courier New" color="#0000ff" size="1"></</font><font
face="Courier New" color="#800000" size="1">html</font><font
face="Courier New" color="#0000ff" size="1">></font></font></font></p>
</div>
</div>
</blockquote>
//----------------------------------------<br>
ljb wrote:<br>
<pre wrap=""><a class="moz-txt-link-abbreviated" href="mailto:noel.faux [at] med.monash.edu.au">noel.faux [at] med.monash.edu.au</a> wrote:
</pre>
<blockquote type="cite">
<pre wrap="">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)?>)>
</pre>
</blockquote>
<pre wrap=""><!---->> ...
>
> 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.</pre>
<br>
<br>
<pre class="moz-signature" cols="72">--
Noel Faux
Department of Biochemistry and Molecular Biology
Monash University
Clayton 3168
Victoria
Australia
</pre>
</body>
</html>
--Boundary_(ID_guCDxg5vhcnlCQv3p5oAOw)--