What does this class do?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • cpweb
    New Member
    • Jun 2007
    • 12

    What does this class do?

    [PHP]

    <?php
    class fkings_DB{
    //+============== =============== =============== ==========+
    function sql_connect($da tabase){
    $this->connect_id = mysql_connect ("localhost" , "root", "jock");
    if($this->connect_id){
    if (mysql_select_d b($database)){
    return $this->connect_id;
    }else{
    return $this->error();
    }
    }else{
    return $this->error();
    }
    }
    //+============== =============== =============== ==========+
    function error(){
    if(mysql_error( ) != ''){
    echo '<h1>MySQL Error</h1>: '.mysql_error() .'<br/>';
    }
    }
    //+============== =============== =============== ==========+

    function query($qry){
    if(!isset($this->connect_id))
    $this->connect();
    $result = mysql_query($qr y, $this->connect_id) or die("Error: ". mysql_error());
    $returnArray = array();
    $i=0;
    while ($row = mysql_fetch_arr ay($result, MYSQL_BOTH))
    if ($row)
    $returnArray[$i++]=$row;
    mysql_free_resu lt($result);
    return $returnArray;
    }

    //+============== =============== =============== ==========+

    function iquery($qry){
    if(!isset($this->connect_id)) $this->connect();
    $temp = mysql_query($qr y, $this->connect_id) or die("Error: ". mysql_error());
    }


    //+============== =============== =============== ==========+
    function get_num_rows($q uery_id = ""){
    if($query_id == NULL){
    $return = mysql_num_rows( $this->query_result );
    }else{
    $return = mysql_num_rows( $query_id);
    }
    if(!$return){
    $this->error();
    }else{
    return $return;
    }
    }
    //+============== =============== =============== ==========+
    function fetch_row($quer y_id = ""){
    if($query_id == NULL){
    $return = mysql_fetch_arr ay($this->query_result );
    }else{
    $return = mysql_fetch_arr ay($query_id);
    }
    if(!$return){
    $this->error();
    }else{
    return $return;
    }
    }
    //+============== =============== =============== ==========+
    function get_affected_ro ws($query_id = ""){
    if($query_id == NULL){
    $return = mysql_affected_ rows($this->query_result );
    }else{
    $return = mysql_affected_ rows($query_id) ;
    }
    if(!$return){
    $this->error();
    }else{
    return $return;
    }
    }
    //+============== =============== =============== ==========+
    function sql_close(){
    if($this->connect_id){
    return mysql_close($th is->connect_id);
    }
    }
    //+============== =============== =============== ==========+
    }
    ?>
    [/PHP]
  • pbmods
    Recognized Expert Expert
    • Apr 2007
    • 5821

    #2
    Changed thread title to better describe the problem (did you know that threads whose titles contain phrases such as 'does anyone care to' actually get FEWER responses?).

    Heya, cpweb.

    It's a DAL ('Data Abstraction Layer'). It encapsulates MySQL functions so that you don't have to directly interact with the database from your code. For more information, check out this article.

    Comment

    • cpweb
      New Member
      • Jun 2007
      • 12

      #3
      Originally posted by pbmods
      Changed thread title to better describe the problem (did you know that threads whose titles contain phrases such as 'does anyone care to' actually get FEWER responses?).

      Heya, cpweb.

      It's a DAL ('Data Abstraction Layer'). It encapsulates MySQL functions so that you don't have to directly interact with the database from your code. For more information, check out this article.


      How can I utilize every function?

      Comment

      • pbmods
        Recognized Expert Expert
        • Apr 2007
        • 5821

        #4
        Heya, cpweb.

        Originally posted by cpweb
        How can I utilize every function?
        You'll first need to instantiate the class:
        [code=php]
        if(! (isset($GLOBALS['db']) && ($GLOBALS['db'] instanceof fkings_DB))) {
        $GLOBALS['db'] = new fkings_DB();
        $GLOBALS['db']->sql_connect('n ameOfYourDataba seGoesHere');
        }
        [/code]

        You may also need to edit the sql_connect() function in fkings_DB to update the username and password (you should create a MySQL User for your web app, and its password should not be 'jock').

        To run a SELECT query, you would then call:

        [code=php]
        $data = $GLOBALS['db']->query('SELEC T query goes here');
        // Do something with $data.
        [/code]

        To use any of the other functions, you use iquery:

        [code=php]
        $result = $GLOBALS['db']->iquery('UPDA TE or DELETE query might go here');
        $data = $GLOBALS['db']->get_affected_r ows($result);
        [/code]

        Comment

        • cpweb
          New Member
          • Jun 2007
          • 12

          #5
          Do I instantiate in the class or my php page?
          Originally posted by pbmods
          Heya, cpweb.



          You'll first need to instantiate the class:
          [code=php]
          if(! (isset($GLOBALS['db']) && ($GLOBALS['db'] instanceof fkings_DB))) {
          $GLOBALS['db'] = new fkings_DB();
          $GLOBALS['db']->sql_connect('n ameOfYourDataba seGoesHere');
          }
          [/code]

          You may also need to edit the sql_connect() function in fkings_DB to update the username and password (you should create a MySQL User for your web app, and its password should not be 'jock').

          To run a SELECT query, you would then call:

          [code=php]
          $data = $GLOBALS['db']->query('SELEC T query goes here');
          // Do something with $data.
          [/code]

          To use any of the other functions, you use iquery:

          [code=php]
          $result = $GLOBALS['db']->iquery('UPDA TE or DELETE query might go here');
          $data = $GLOBALS['db']->get_affected_r ows($result);
          [/code]

          Comment

          • ronnil
            Recognized Expert New Member
            • Jun 2007
            • 134

            #6
            $GLOBALS['db'] = new fkings_DB();

            is how you instantiate the class

            these two following links provide all the information you need to work with classes/objects in PHP

            http://www.php.net/manual/en/language.oop.php http://www.php.net/manual/en/language.oop5.php

            premused ofc you already know the basics of working with objects

            Comment

            Working...