Go Back   Forum Care Forums > Development Reference Area > MySQL Discussion

Reply
 
LinkBack Thread Tools Display Modes
Connections in OOP
Old
  (#1)
Schmidty
Guest
 
Posts: n/a
Default Connections in OOP - 06-04-2007, 07:51 AM

I am a newbie with MySQL(actually MySQLI), PHP5 and OOP and had a
question that probably relates to other groups but since this group is
heavily trafficated I thought someone might have an answer here;

When I refresh/reload a page in PHP using $_SERVER['PHP_SELF'] I
believe that I am losing the database connection or I am not properly
applying my OOP skills when using MySQLI and cl***es and methods. How
do I keep a connection even with a page refresh and moving between a
script and the cl***?

I have a members page that uses an 'admin.cl***.php' with the admin
cl*** and the database connection function in it. I seem to lose the
connection when moving from the constructor into another function. Is
this what a persistent connection does? I know that MySQLI doesn't
support persistent connections and to be honest, from what I have read,
I don't think I want a persistent connection. So how do I keep the
connections alive between methods (functions) in my cl***?

Example Code
================================================== ===========
<?php
// admin.cl***.php
cl*** admin{
public $file_path;
public $user;
private $p***;
public $result;
public $mysqli;

function showNewInfo(){
$this->dbConnect();
$query1 = "SELECT userID, fname, minit, lname, address, city, state,
zip, phone, useremail, ctype, cnumb, exp, recdate FROM personalinfo
WHERE checked = FALSE";
if ($result = $this->mysqli->real_query($query1)){
$result = $this->mysqli->store_result();
if($this->mysqli->error) { echo "ERROR with mysqli stored procedure";}
} // end if
// Show info of users not updated
echo "<form action='".$_SERVER['PHP_SELF']."' method='post'><table
style=\"font: 14px solid #000000; border: 2px solid #CCC000;\">";
echo "<tr><td colspan='2' style=\"background: #C0C0C5; text-align:
center; font: 16px solid #000000;\">Credit Card
Applications</td></tr>";
echo "<tr><td colspan='2' style=\"background: #FFFFE0; text-align:
left; font: 11px solid #000000;\">Current info in database:</td></tr>";
echo "<tr><tr><td colspan='2' style=\"border: 2px solid #000000;
width: 100%;\">";
while($row = $result->fetch_***oc()){
echo "<input type='checkbox' name='perinfo[]'
value='".$row['userID']."' >".$row['userID']." : ".$row['lname']." -
".$row['recdate']."<br />";
} // end while
echo "</td></tr><tr>";
echo "<td style=\"align: center;\"><input style=\"font: 10px solid
#000000;\" type=\"submit\" value=\"UPDATE INFO\" name='updatedb'
/></td><td style=\"text-align: center;\"><input type=\"reset\"
style=\"font: 10px solid #000000;\" value=\"RESET CHOICES\"
/></td></tr></form></table>";
$result->free();
$this->dbClose();
} // end function

function dbConnect(){
include('dbconn.php');
$mysqli = new mysqli($dbnet, $dbuser, $dbp***, $dbname) or die("ERROR:
Cannot connect to database server");
if (mysqli_connect_errno()){ printf("Can't connect to MySQL Server.
Errorcode: %s\n", mysqli_connect_error());
exit;
} // end if
$this->mysqli = $mysqli;

} // end function dbConnect

function dbClose(){
$this->mysqli->close();
} // end function dbClose

function updateDB($perinfo){
foreach($perinfo as $info);
$query3= "UPDATE personalinfo SET checked = 'TRUE' WHERE userID =
$info";
if (!$this->mysqli) {echo "ERROR on mysqli"; exit;}
$result = $this->mysqli->query($query3);
echo "QUERY DONE";
$result->free();
return true;
} // end function updateDB
} // end cl***

End Example Code
================================================== ==============

Thanks for any help anyone might provide! Like I said; I am still
learning this stuff!!!!

Schmidty

   
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Re: Connections in OOP
Old
  (#2)
Jerry Stuckle
Guest
 
Posts: n/a
Default Re: Connections in OOP - 06-04-2007, 07:51 AM

Schmidty wrote:
> I am a newbie with MySQL(actually MySQLI), PHP5 and OOP and had a
> question that probably relates to other groups but since this group is
> heavily trafficated I thought someone might have an answer here;
>
> When I refresh/reload a page in PHP using $_SERVER['PHP_SELF'] I
> believe that I am losing the database connection or I am not properly
> applying my OOP skills when using MySQLI and cl***es and methods. How
> do I keep a connection even with a page refresh and moving between a
> script and the cl***?
>
> I have a members page that uses an 'admin.cl***.php' with the admin
> cl*** and the database connection function in it. I seem to lose the
> connection when moving from the constructor into another function. Is
> this what a persistent connection does? I know that MySQLI doesn't
> support persistent connections and to be honest, from what I have read,
> I don't think I want a persistent connection. So how do I keep the
> connections alive between methods (functions) in my cl***?
>
> Example Code
> ================================================== ===========
> <?php
> // admin.cl***.php
> cl*** admin{
> public $file_path;
> public $user;
> private $p***;
> public $result;
> public $mysqli;
>
> function showNewInfo(){
> $this->dbConnect();
> $query1 = "SELECT userID, fname, minit, lname, address, city, state,
> zip, phone, useremail, ctype, cnumb, exp, recdate FROM personalinfo
> WHERE checked = FALSE";
> if ($result = $this->mysqli->real_query($query1)){
> $result = $this->mysqli->store_result();
> if($this->mysqli->error) { echo "ERROR with mysqli stored procedure";}
> } // end if
> // Show info of users not updated
> echo "<form action='".$_SERVER['PHP_SELF']."' method='post'><table
> style=\"font: 14px solid #000000; border: 2px solid #CCC000;\">";
> echo "<tr><td colspan='2' style=\"background: #C0C0C5; text-align:
> center; font: 16px solid #000000;\">Credit Card
> Applications</td></tr>";
> echo "<tr><td colspan='2' style=\"background: #FFFFE0; text-align:
> left; font: 11px solid #000000;\">Current info in database:</td></tr>";
> echo "<tr><tr><td colspan='2' style=\"border: 2px solid #000000;
> width: 100%;\">";
> while($row = $result->fetch_***oc()){
> echo "<input type='checkbox' name='perinfo[]'
> value='".$row['userID']."' >".$row['userID']." : ".$row['lname']." -
> ".$row['recdate']."<br />";
> } // end while
> echo "</td></tr><tr>";
> echo "<td style=\"align: center;\"><input style=\"font: 10px solid
> #000000;\" type=\"submit\" value=\"UPDATE INFO\" name='updatedb'
> /></td><td style=\"text-align: center;\"><input type=\"reset\"
> style=\"font: 10px solid #000000;\" value=\"RESET CHOICES\"
> /></td></tr></form></table>";
> $result->free();
> $this->dbClose();
> } // end function
>
> function dbConnect(){
> include('dbconn.php');
> $mysqli = new mysqli($dbnet, $dbuser, $dbp***, $dbname) or die("ERROR:
> Cannot connect to database server");
> if (mysqli_connect_errno()){ printf("Can't connect to MySQL Server.
> Errorcode: %s\n", mysqli_connect_error());
> exit;
> } // end if
> $this->mysqli = $mysqli;
>
> } // end function dbConnect
>
> function dbClose(){
> $this->mysqli->close();
> } // end function dbClose
>
> function updateDB($perinfo){
> foreach($perinfo as $info);
> $query3= "UPDATE personalinfo SET checked = 'TRUE' WHERE userID =
> $info";
> if (!$this->mysqli) {echo "ERROR on mysqli"; exit;}
> $result = $this->mysqli->query($query3);
> echo "QUERY DONE";
> $result->free();
> return true;
> } // end function updateDB
> } // end cl***
>
> End Example Code
> ================================================== ==============
>
> Thanks for any help anyone might provide! Like I said; I am still
> learning this stuff!!!!
>
> Schmidty
>


Schmidty,

If I understand you correctly, this is normal operation.

Basically, each page can be considered a program in itself, started when
the page is requested. When any page is loaded (via refresh/reload or
otherwise), everything starts out fresh.

Let's say the user opens a page. Your code starts up, establishes a
connection, and does some work. It then completes its processing and
sends the page to the user.

As part of the page completion, all database connections, etc. are
terminated and your objects are released. So there are no connections
and no objects left. Persistent connections have nothing to do with this.

When the new page comes up, you have to start all over. If you want to
access the database, you need to establish a new connection.

As this is not a MySQL issue, if you have more questions I recommend you
follow up in comp.lang.php.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
EMAIL REMOVED
==================
   
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Re: Connections in OOP
Old
  (#3)
Schmidty
Guest
 
Posts: n/a
Default Re: Connections in OOP - 06-04-2007, 07:51 AM

Thanks Jerry for your quick reply!

I wasn't sure if I should ask this question with MySQL group or the PHP
group or an OOP PHP group...I will ask the PHP group. Thanks for the
info...

Schmidty


Jerry Stuckle wrote:
> Schmidty wrote:
> > I am a newbie with MySQL(actually MySQLI), PHP5 and OOP and had a
> > question that probably relates to other groups but since this group is
> > heavily trafficated I thought someone might have an answer here;
> >
> > When I refresh/reload a page in PHP using $_SERVER['PHP_SELF'] I
> > believe that I am losing the database connection or I am not properly
> > applying my OOP skills when using MySQLI and cl***es and methods. How
> > do I keep a connection even with a page refresh and moving between a
> > script and the cl***?
> >
> > I have a members page that uses an 'admin.cl***.php' with the admin
> > cl*** and the database connection function in it. I seem to lose the
> > connection when moving from the constructor into another function. Is
> > this what a persistent connection does? I know that MySQLI doesn't
> > support persistent connections and to be honest, from what I have read,
> > I don't think I want a persistent connection. So how do I keep the
> > connections alive between methods (functions) in my cl***?
> >
> > Example Code
> > ================================================== ===========
> > <?php
> > // admin.cl***.php
> > cl*** admin{
> > public $file_path;
> > public $user;
> > private $p***;
> > public $result;
> > public $mysqli;
> >
> > function showNewInfo(){
> > $this->dbConnect();
> > $query1 = "SELECT userID, fname, minit, lname, address, city, state,
> > zip, phone, useremail, ctype, cnumb, exp, recdate FROM personalinfo
> > WHERE checked = FALSE";
> > if ($result = $this->mysqli->real_query($query1)){
> > $result = $this->mysqli->store_result();
> > if($this->mysqli->error) { echo "ERROR with mysqli stored procedure";}
> > } // end if
> > // Show info of users not updated
> > echo "<form action='".$_SERVER['PHP_SELF']."' method='post'><table
> > style=\"font: 14px solid #000000; border: 2px solid #CCC000;\">";
> > echo "<tr><td colspan='2' style=\"background: #C0C0C5; text-align:
> > center; font: 16px solid #000000;\">Credit Card
> > Applications</td></tr>";
> > echo "<tr><td colspan='2' style=\"background: #FFFFE0; text-align:
> > left; font: 11px solid #000000;\">Current info in database:</td></tr>";
> > echo "<tr><tr><td colspan='2' style=\"border: 2px solid #000000;
> > width: 100%;\">";
> > while($row = $result->fetch_***oc()){
> > echo "<input type='checkbox' name='perinfo[]'
> > value='".$row['userID']."' >".$row['userID']." : ".$row['lname']." -
> > ".$row['recdate']."<br />";
> > } // end while
> > echo "</td></tr><tr>";
> > echo "<td style=\"align: center;\"><input style=\"font: 10px solid
> > #000000;\" type=\"submit\" value=\"UPDATE INFO\" name='updatedb'
> > /></td><td style=\"text-align: center;\"><input type=\"reset\"
> > style=\"font: 10px solid #000000;\" value=\"RESET CHOICES\"
> > /></td></tr></form></table>";
> > $result->free();
> > $this->dbClose();
> > } // end function
> >
> > function dbConnect(){
> > include('dbconn.php');
> > $mysqli = new mysqli($dbnet, $dbuser, $dbp***, $dbname) or die("ERROR:
> > Cannot connect to database server");
> > if (mysqli_connect_errno()){ printf("Can't connect to MySQL Server.
> > Errorcode: %s\n", mysqli_connect_error());
> > exit;
> > } // end if
> > $this->mysqli = $mysqli;
> >
> > } // end function dbConnect
> >
> > function dbClose(){
> > $this->mysqli->close();
> > } // end function dbClose
> >
> > function updateDB($perinfo){
> > foreach($perinfo as $info);
> > $query3= "UPDATE personalinfo SET checked = 'TRUE' WHERE userID =
> > $info";
> > if (!$this->mysqli) {echo "ERROR on mysqli"; exit;}
> > $result = $this->mysqli->query($query3);
> > echo "QUERY DONE";
> > $result->free();
> > return true;
> > } // end function updateDB
> > } // end cl***
> >
> > End Example Code
> > ================================================== ==============
> >
> > Thanks for any help anyone might provide! Like I said; I am still
> > learning this stuff!!!!
> >
> > Schmidty
> >

>
> Schmidty,
>
> If I understand you correctly, this is normal operation.
>
> Basically, each page can be considered a program in itself, started when
> the page is requested. When any page is loaded (via refresh/reload or
> otherwise), everything starts out fresh.
>
> Let's say the user opens a page. Your code starts up, establishes a
> connection, and does some work. It then completes its processing and
> sends the page to the user.
>
> As part of the page completion, all database connections, etc. are
> terminated and your objects are released. So there are no connections
> and no objects left. Persistent connections have nothing to do with this.
>
> When the new page comes up, you have to start all over. If you want to
> access the database, you need to establish a new connection.
>
> As this is not a MySQL issue, if you have more questions I recommend you
> follow up in comp.lang.php.
>
> --
> ==================
> Remove the "x" from my email address
> Jerry Stuckle
> JDS Computer Training Corp.
> EMAIL REMOVED
> ==================


   
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On





Contact Us - Forum Care Forums - Archive - Top