Re: mysqli connections and oop





On Jan 24, 1:26 am, Erwin Moller
<since_humans_read_this_I_am_spammed_too_m...@xxxxxxxxxxxxxxxx> wrote:
Schmidty wrote:
So if I do a $_SERVER['PHP_SELF'] in a function within a class that
will end the execution of that class and restart the same script? Do I
need to do all processing in a script before I display a page that will
have a $_SERVER['PHP_SELF'] action? Thanks for the help...

SchmidtyHi,

Please don't toppost. It destroys the order of the conversation.
$_SERVER['PHP_SELF'] is just a string that contains the script that is being
executed.
I do not understand what 'doing a $_SERVER['PHP_SELF'] in a function' means.
But it will most probably NOT terminated the execution of the script.
exit(); does.

You must clearly see the difference between the server and the client
(browser).

Your script produces a form that will be send back to the client (=browser).
Then your script ends.
The form action in your example posts back to the same script (because you
used the target for the action $_SERVER['PHP_SELF']), so if anybody presses
submit, that form will be posted, and hence the same script will be
executed as before.
But the server or PHP couldn't care less, and thinks of it as just another
call to that script.

http-protocol is stateless, this means that NO lasting connection is build
between the client and the server. To overcome this you must use sessions.

And as Jerry said: connections to a database are NOT fit to store in a
session because PHP destroys all existing connections when the script ends.
So you'll have to rebuild your connection each invocation of a script that
needs a database.

Hope this helps.

Regards,
Erwin Moller





On Jan 23, 4:54 pm, Jerry Stuckle <jstuck...@xxxxxxxxxxxxx> wrote:
Schmidty wrote:
If you do a page reload with $_SERVER['PHP_SELF'] will your program
lose a mysqli connection upon the reload of the page? Would this code
work? I need to know how to carry over a connection between methods as
I am new to OOP? Thanks...

Example; ========================================
<?php
// webpage
$newsignon = new newuser();
logon();

if (isset($_POST['submit'])){
$newsignon = query("SELECT name, password FROM
database");
}
?>
<?php
// class newuser

class newuser {
public $name;
private $passwd;
private $mysqli;

function __construct(){
include('dbconn.php');
$mysqli = new mysqli($dbnet, $dbuser, $dbpass, $dbname);
if (mysqli_connect_errno()){ printf("Can't connect to MySQL
Server. Errorcode: %s\n", mysqli_connect_error());
exit;
}
$this->mysqli = $mysqli; // is this right?
} //end constructor

function query($query){
if ($result = $this->mysqli->real_query($query)){
$result = $this->mysqli->store_result();
if($this->mysqli->error) { echo "ERROR with mysqli stored
procedure";}
} // end if

} // end function

function logon(){
echo "<form action='".$_POST['PHP_SELF']."' method='post' >";
// Would this work?
echo "<input type='text' name='user' /><br /><input
type='password' name='passwd' />";
echo "<input type='submit' value='submit' />";

} // end function

} //end classSchmidty,

As I explained in comp.database.mysql, you can't. Once a page's
processing is completed, all connections (and objects) are released.

You can keep information like user ids, etc. in the $_SESSION variable,
but not the connection. Even if you could keep the connection info in
there, the connection itself will be closed.

Each page is it's own little program. Any external resources opened by
it are closed at the end of the page processing.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstuck...@xxxxxxxxxxxxx
==================- Hide quoted text -- Show quoted text -

Thanks Erwin

The Google Groups editor places your reponse at the top when making a
reply. I went ahead posted at the bottom for you...

I understand that HTTP is stateless and I have been using sessions
variables and understand that you can't carry a database connection
through either one. I was confused on what happens with an
$_SERVER['HTTP_SELF'] in a script. What I meant by "doing a
$_SERVER['PHP_SELF'] in a function" is having a page echoed in a
function and having the 'action' of that page execute a
$_SERVER['HTTP_SELF']. Does it end a mysqli connection even though you
have not exited the script? Your explanation that $_SERVER['PHP_SELF']
is just a string that contains the script that is being executed makes
sense. I wasn't sure if the connection was being terminated by the
$_SERVER['HTTP_SELF'] variable. Thanks for the help in understanding
this. :)

Schmidty

.



Relevant Pages

  • Re: [PHP] PHP console script vs C/C++/C#
    ... My script is taking a longer time to execute than I want. ... I prefer to write in PHP because that is what I know best. ... This is why I am thinking about rewriting my whole script in a C language. ... Perhaps there are different methods I could be using to speed up execution. ...
    (php.general)
  • Re: [PHP] PHP console script vs C/C++/C#
    ... My script is taking a longer time to execute than I want. ... I prefer to write in PHP because that is what I know best. ... thinking about rewriting my whole script in a C language. ... execution of the code. ...
    (php.general)
  • Re: PHPMailer - False Positives
    ... My problem is that the PHPMailer action is returning a "true" result ... Initially the script created a new object for each email (sending them ... each execution), but the fault was evident in each configuration. ... PHP Classes - Free ready to use OOP components written in PHP ...
    (comp.lang.php)
  • RE: [PHP] Closing a connection to browser without exiting the script
    ... Closing a connection to browser without exiting the ... I have a PHP page that displays a message, and then, performs a very ... what I would like is to be able to close the connection from ... the server-side, but without using the exitfunction, so my script ...
    (php.general)
  • Re: Have you heard about a MySQL connection leak?
    ... This doesn't change with scripting languages like PHP. ... Anyway, the reason a TCP socket connection stays open, is because neither end has closed it. ... Normally a PHP script closes all open connections when it exits. ... When an endpoint wishes to stop its half of the connection, it transmits a FIN packet, which the other end acknowledges with an ACK. ...
    (comp.lang.php)