[PHP] Re: newbie questions

2007-10-21 Thread M. Sokolewicz

Ravi wrote:


Guys, I am fairly new to PHP. Here are a few questions, if anybody can 
answer it will help me get started. Thanks


I am trying to build a website and I would like to do the following in 
my scripts


1. I want to return response to the browser and AFTERWARDS make a log 
entry in to a database. I need this so user can experience a fast response.
There is no before and after. Everything you do happens during (part 
of) the response. But you can just output your data, whatever it may be, 
flush() it and then log it via the same script. Your user won't notice a 
thing (Hell, even without the flush your user won't notice it probably).


2. If the database update fails, I want to ignore it (since it is just 
log entry). Something like try-catch construct in Java. This is more 
important if item1 mentioned above is not possible. Essentially whether 
I make a database entry or not, I must return a valid response to user.
So ignore it :) If you don't check for errors, you won't see them... 
Makes debugging very annoying, but you won't see em nevertheless. If 
your output is not based on anything from your database-update, then 
there apparently is no need to worry about it.


3. Is there something like connection pool in php? Do usually people 
open/close database connection for every request (I doubt that, it 
sounds really slow).
There is something like that, the persistent connections (ie. via 
mysql_pconnect), but generally people DO open/close connections via the 
same script each and every time the script is executed (this might sound 
very slow, but it's actually not too bad). Using persistent connections 
is not always the best option (and usually doesn't even make much 
sense); there's a good bit of documentation about it in the php docs:

http://www.php.net/manual/en/features.persistent-connections.php

Some code samples or pointers to documentation for the above would also 
be very helpful.

code samples of what exactly ?


Thanks
Ravi


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[PHP] Re: newbie questions

2007-10-21 Thread Ravi


That was very very helpful. Thanks a ton!

One more question. For every request, I am sending a redirect back to 
the user and the browser takes the user to another url. The problem is 
that the browser is not redirecting until the script finishes. Even if I 
do flush(), the browser waits til script ends. Is there a way to force 
browser to redirect and not wait for the script to end?


In Java I can think of many ways, one is to use threads, hand of data to 
another thread and return the response. Another solution would be to 
store data in memory (static variable) and update only after every 100 
requests.


Is any of this possible in PHP?


M. Sokolewicz wrote:

Ravi wrote:


Guys, I am fairly new to PHP. Here are a few questions, if anybody can 
answer it will help me get started. Thanks


I am trying to build a website and I would like to do the following in 
my scripts


1. I want to return response to the browser and AFTERWARDS make a log 
entry in to a database. I need this so user can experience a fast 
response.
There is no before and after. Everything you do happens during (part 
of) the response. But you can just output your data, whatever it may be, 
flush() it and then log it via the same script. Your user won't notice a 
thing (Hell, even without the flush your user won't notice it probably).


2. If the database update fails, I want to ignore it (since it is just 
log entry). Something like try-catch construct in Java. This is more 
important if item1 mentioned above is not possible. Essentially 
whether I make a database entry or not, I must return a valid response 
to user.
So ignore it :) If you don't check for errors, you won't see them... 
Makes debugging very annoying, but you won't see em nevertheless. If 
your output is not based on anything from your database-update, then 
there apparently is no need to worry about it.


3. Is there something like connection pool in php? Do usually people 
open/close database connection for every request (I doubt that, it 
sounds really slow).
There is something like that, the persistent connections (ie. via 
mysql_pconnect), but generally people DO open/close connections via the 
same script each and every time the script is executed (this might sound 
very slow, but it's actually not too bad). Using persistent connections 
is not always the best option (and usually doesn't even make much 
sense); there's a good bit of documentation about it in the php docs:

http://www.php.net/manual/en/features.persistent-connections.php

Some code samples or pointers to documentation for the above would 
also be very helpful.

code samples of what exactly ?


Thanks
Ravi




--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Re: newbie questions

2007-10-21 Thread Richard Heyes

Ravi wrote:


That was very very helpful. Thanks a ton!

One more question. For every request, I am sending a redirect back to 
the user and the browser takes the user to another url. The problem is 
that the browser is not redirecting until the script finishes. Even if I 
do flush(), the browser waits til script ends. Is there a way to force 
browser to redirect and not wait for the script to end?


In Java I can think of many ways, one is to use threads, hand of data to 
another thread and return the response. Another solution would be to 
store data in memory (static variable) and update only after every 100 
requests.


Not having read the rest of the thread, you could call exit just after 
the redirect header is sent, eg:


?php
header('Location: http://www.yahoo.com');
exit;
?

Richard Heyes
+44 (0)800 0213 172
http://www.websupportsolutions.co.uk

Knowledge Base and HelpDesk software
that can cut the cost of online support

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Re: newbie questions

2007-10-21 Thread Ravi


Richard, unfortunately I cannot end the script. I need something like this:

?php
header('Location: http://www.yahoo.com');
// somehow let the browser move to yahoo.com
// now update the database to store some information about user
exit;
?


Richard Heyes wrote:

Ravi wrote:


That was very very helpful. Thanks a ton!

One more question. For every request, I am sending a redirect back to 
the user and the browser takes the user to another url. The problem is 
that the browser is not redirecting until the script finishes. Even if 
I do flush(), the browser waits til script ends. Is there a way to 
force browser to redirect and not wait for the script to end?


In Java I can think of many ways, one is to use threads, hand of data 
to another thread and return the response. Another solution would be 
to store data in memory (static variable) and update only after every 
100 requests.


Not having read the rest of the thread, you could call exit just after 
the redirect header is sent, eg:


?php
header('Location: http://www.yahoo.com');
exit;
?

Richard Heyes
+44 (0)800 0213 172
http://www.websupportsolutions.co.uk

Knowledge Base and HelpDesk software
that can cut the cost of online support



--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Re: newbie questions

2007-10-21 Thread Richard Heyes

Ravi wrote:


Richard, unfortunately I cannot end the script. I need something like this:

?php
header('Location: http://www.yahoo.com');
// somehow let the browser move to yahoo.com
// now update the database to store some information about user
exit;
?


In that case you might want to look at register_shutdown_function().

Richard Heyes
+44 (0)800 0213 172
http://www.websupportsolutions.co.uk

Knowledge Base and HelpDesk software
that can cut the cost of online support

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Re: newbie questions

2007-10-21 Thread Larry Garfield
On Sunday 21 October 2007, Richard Heyes wrote:
 Ravi wrote:
  Richard, unfortunately I cannot end the script. I need something like
  this:
 
  ?php
  header('Location: http://www.yahoo.com');
  // somehow let the browser move to yahoo.com
  // now update the database to store some information about user
  exit;
  ?

 In that case you might want to look at register_shutdown_function().

That would work, but I think you're probably not approaching the question 
properly.  Why do you need to redirect the user first, then log the request?  
PHP/MySQL are fast enough that logging first and then redirecting will have 
no noticeable impact on performance or your user experience.  (I'm assuming a 
logging process here that's only 1-3 queries.)  It sounds like you're trying 
to over-optimize, which is always a bad idea as it makes the code harder to 
understand later. :-)

-- 
Larry Garfield  AIM: LOLG42
[EMAIL PROTECTED]   ICQ: 6817012

If nature has made any one thing less susceptible than all others of 
exclusive property, it is the action of the thinking power called an idea, 
which an individual may exclusively possess as long as he keeps it to 
himself; but the moment it is divulged, it forces itself into the possession 
of every one, and the receiver cannot dispossess himself of it.  -- Thomas 
Jefferson

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[PHP] Re: newbie questions

2007-10-21 Thread Ravi


Maybe you have a point. I will do performance testing and then decide if 
I should try to optimize to that point.


Yes the logging is just one simple insert into the database.

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Re: newbie questions

2007-10-21 Thread Greg Donald
On 10/21/07, Ravi [EMAIL PROTECTED] wrote:
 Maybe you have a point. I will do performance testing and then decide if
 I should try to optimize to that point.

 Yes the logging is just one simple insert into the database.

Does your database support some form of INSERT DELAYED ?

Like MySQL does: http://dev.mysql.com/doc/refman/5.0/en/insert-delayed.html


-- 
Greg Donald
http://destiney.com/

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Re: newbie questions

2007-10-21 Thread Larry Garfield
I will bet you money that there are far better places to optimize your 
application than moving a single SQL insert to after the final output.

On Sunday 21 October 2007, Ravi wrote:
 Maybe you have a point. I will do performance testing and then decide if
 I should try to optimize to that point.

 Yes the logging is just one simple insert into the database.


-- 
Larry Garfield  AIM: LOLG42
[EMAIL PROTECTED]   ICQ: 6817012

If nature has made any one thing less susceptible than all others of 
exclusive property, it is the action of the thinking power called an idea, 
which an individual may exclusively possess as long as he keeps it to 
himself; but the moment it is divulged, it forces itself into the possession 
of every one, and the receiver cannot dispossess himself of it.  -- Thomas 
Jefferson

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[PHP] Re: newbie questions

2004-08-29 Thread Torsten Roehr
Kevin Bridges [EMAIL PROTECTED] wrote in message
news:[EMAIL PROTECTED]
 Greetings php-general,

  I'm just starting out with php ... judging from the posts I've been
  reading on this list I'm thinking this question is so basic it might
  almost be pathetic! I'm assuming the answers are in the php manual,
  but I have not found the correct pages ... any pointers to manual
  pages, tutorials, or an explanation would be greatly appreciated.

 ?php
 require_once 'library/functions/dbConnect.php';
 class Content {
  var $db = null; // PEAR::DB pointer
  function Content($db) {
   $this-db = $db;
  }
 }
 ?

  I'm writing my first class following an example I found on the web
  and I have 2 questions.  The constructor for Content accepts an
  object ... why is $db used instead of $db?

  I'm used to a this.varName syntax from other languages ... I grasp
  what is happening with $this-db, but I don't understand the literal
  translation of - and would like help understanding.

Hi Kevin,

with PHP4 $db means the value is passed by reference. Otherwise a copy of
$db would be passed to the method. See here:
http://de2.php.net/references

With PHP5 variable assignments or passing variables to functions by
reference is standard.

$this-db is the PHP equal to this.varName. You access property db of the
current object. Methods are called this way:
$this-methodName()

Hope this clears things up a bit.

Best regards, Torsten Roehr

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php