Professional Web Applications Themes

Connections in OOP - MySQL

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 classes and methods. How do I keep a connection even with a page refresh and moving between a script and the class? I have a members page that uses an 'admin.class.php' with the admin ...

  1. #1

    Default Connections in OOP

    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 classes and methods. How
    do I keep a connection even with a page refresh and moving between a
    script and the class?

    I have a members page that uses an 'admin.class.php' with the admin
    class 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 class?

    Example Code
    ================================================== ===========
    <?php
    // admin.class.php
    class admin{
    public $file_path;
    public $user;
    private $pass;
    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_assoc()){
    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, $dbpass, $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 class

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

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

    Schmidty

    Schmidty Guest

  2. #2

    Default Re: Connections in OOP

    Schmidty wrote: 

    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.
    net
    ==================
    Jerry Guest

  3. #3

    Default Re: Connections in OOP

    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,
    >
    > 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.
    > net
    > ==================[/ref]

    Schmidty Guest

Similar Threads

  1. Connections?
    By micheSWE in forum Macromedia Flash Flashcom
    Replies: 0
    Last Post: August 25th, 08:03 AM
  2. RDP connections
    By David in forum Windows Server
    Replies: 0
    Last Post: July 12th, 06:19 PM
  3. Too Many Connections
    By James Hall in forum ASP Database
    Replies: 2
    Last Post: January 8th, 02:37 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139