Connect & Disconnetcting functions

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • knoak

    Connect & Disconnetcting functions

    Hi there,

    I'm building a DB-driven site, and i have one page (settings.php) that
    is included on each viewable page. In settings.php i have a few
    functions, so i do not have to type them all, or modify them all if
    anything changes..

    For the same reason i want to create a function to connect to a mySQL
    DB.
    What i have is:

    ############### ############### ############### ##
    function doConnect(){
    $db = mysql_connect(" host", "user", "pass");
    mysql_select_db ("data",$db) ;
    };

    function doDisConnect(){
    mysql_close($db );
    };
    ############### ############### ############### ##

    And then i want to run them from a page where settings.php is
    included:

    _______________ _______________ _______________ __
    doConnect();

    $get_user = mysql_query("SE LECT id, customerID, hits FROM
    customerID WHERE customerID='$us er'",$db);

    if ($data = mysql_fetch_arr ay($get_user)) {
    $sql = "UPDATE customerID SET hits = hits + 1 WHERE
    customerID ='$user'";
    }else{
    $sql = "INSERT INTO customerID (customerID, hits) VALUES
    ('$user', 1)";
    };
    $addhit = mysql_query($sq l);

    doDisConnect();
    _______________ _______________ _______________ __

    this function is a counter, but that isn't really important. Why can't
    i get this to work? Or should i do it totally different?

    I'd like to find out now, before i have used it in a lot of pages...

    Thanks in advance. Greetings knoakske
  • Sadara

    #2
    Re: Connect & Disconnetcting functions

    knoak wrote:[color=blue]
    > Hi there,
    >
    > I'm building a DB-driven site, and i have one page (settings.php) that
    > is included on each viewable page. In settings.php i have a few
    > functions, so i do not have to type them all, or modify them all if
    > anything changes..
    >
    > For the same reason i want to create a function to connect to a mySQL
    > DB.
    > What i have is:
    >
    > ############### ############### ############### ##
    > function doConnect(){
    > $db = mysql_connect(" host", "user", "pass");
    > mysql_select_db ("data",$db) ;
    > };
    >
    > function doDisConnect(){
    > mysql_close($db );
    > };
    > ############### ############### ############### ##[/color]

    the value of the variable $db is most likely local to the function
    doConnect() and will therefore not be available to the mysql_query call.
    you could either make $db global in scope, or make its value the return
    value of doConnect().

    sadara
    [color=blue]
    >
    > And then i want to run them from a page where settings.php is
    > included:
    >
    > _______________ _______________ _______________ __
    > doConnect();
    >
    > $get_user = mysql_query("SE LECT id, customerID, hits FROM
    > customerID WHERE customerID='$us er'",$db);
    >
    > if ($data = mysql_fetch_arr ay($get_user)) {
    > $sql = "UPDATE customerID SET hits = hits + 1 WHERE
    > customerID ='$user'";
    > }else{
    > $sql = "INSERT INTO customerID (customerID, hits) VALUES
    > ('$user', 1)";
    > };
    > $addhit = mysql_query($sq l);
    >
    > doDisConnect();
    > _______________ _______________ _______________ __
    >
    > this function is a counter, but that isn't really important. Why can't
    > i get this to work? Or should i do it totally different?
    >
    > I'd like to find out now, before i have used it in a lot of pages...
    >
    > Thanks in advance. Greetings knoakske[/color]

    Comment

    • badr

      #3
      Re: Connect & Disconnetcting functions

      hi knoak

      your code??????
      --------------------------------------------------------------
      if ($data = mysql_fetch_arr ay($get_user)) {
      --------------------------------------------------------------
      you may forget == to be
      if ($data == mysql_fetch_arr ay($get_user)) {
      ---------------------------------------------------------------

      but i have to tell you about very practical method in your situation ,
      i think if you have very large project or more than 5 PHP files you may
      use the MySQL Class for connectivity and query purposes check this site
      and you will find a lot of free MySQL Classes

      http://www.weberdev.com/get_example-3733.html (better one i think)



      tell me about the result.

      Comment

      • Richards Noah \(IFR LIT MET\)

        #4
        Re: Connect & Disconnetcting functions


        "knoak" <knoakske@hotma il.com> wrote in message
        news:a5e41e0e.0 501031057.6f7b2 aa5@posting.goo gle.com...[color=blue]
        > Hi there,
        >
        > I'm building a DB-driven site, and i have one page (settings.php) that
        > is included on each viewable page. In settings.php i have a few
        > functions, so i do not have to type them all, or modify them all if
        > anything changes..
        >
        > For the same reason i want to create a function to connect to a mySQL
        > DB.
        > What i have is:
        >
        > ############### ############### ############### ##
        > function doConnect(){
        > $db = mysql_connect(" host", "user", "pass");
        > mysql_select_db ("data",$db) ;
        > };
        >[/color]

        It's a good idea to implement some error checking in here. For example:

        $db = mysql_connect(" host", "user", "pass") or die(mysql_error ());

        Also, mysql_select_db () doesn't need the $db argument, since it defaults to
        the last thing opened by mysql_connect() .
        [color=blue]
        > function doDisConnect(){
        > mysql_close($db );
        > };
        > ############### ############### ############### ##
        >
        > And then i want to run them from a page where settings.php is
        > included:
        >
        > _______________ _______________ _______________ __
        > doConnect();
        >
        > $get_user = mysql_query("SE LECT id, customerID, hits FROM
        > customerID WHERE customerID='$us er'",$db);[/color]

        You don't need to include the $db here if you are using mysql_select_db ()
        in doConnect. mysql_query() will default to that.
        [color=blue]
        > if ($data = mysql_fetch_arr ay($get_user)) {
        > $sql = "UPDATE customerID SET hits = hits + 1 WHERE
        > customerID ='$user'";
        > }else{
        > $sql = "INSERT INTO customerID (customerID, hits) VALUES
        > ('$user', 1)";
        > };
        > $addhit = mysql_query($sq l);
        >
        > doDisConnect();
        > _______________ _______________ _______________ __
        >
        > this function is a counter, but that isn't really important. Why can't
        > i get this to work? Or should i do it totally different?
        >
        > I'd like to find out now, before i have used it in a lot of pages...
        >
        > Thanks in advance. Greetings knoakske[/color]

        Hopefully that helps. If you still have problems, post what exactly is
        going wrong so we can help diagnose the problem and propose more meaningful
        solutions.


        Comment

        • Dani CS

          #5
          Re: Connect &amp; Disconnetcting functions

          badr wrote:[color=blue]
          > hi knoak
          >
          > your code??????
          > --------------------------------------------------------------
          > if ($data = mysql_fetch_arr ay($get_user)) {
          > --------------------------------------------------------------
          > you may forget == to be
          > if ($data == mysql_fetch_arr ay($get_user)) {
          > ---------------------------------------------------------------[/color]

          Normally your comment about = and == is useful. But this is not the
          case. The original post wants to make the assignment and then test the
          result, thus having the $data available inside the if {} block.

          <snip>

          Comment

          • Richards Noah \(IFR LIT MET\)

            #6
            Re: Connect &amp; Disconnetcting functions


            "badr" <DWEBD@msn.co m> wrote in message
            news:1104784723 .620505.28770@z 14g2000cwz.goog legroups.com...[color=blue]
            > hi knoak
            >
            > your code??????
            > --------------------------------------------------------------
            > if ($data = mysql_fetch_arr ay($get_user)) {
            > --------------------------------------------------------------
            > you may forget == to be
            > if ($data == mysql_fetch_arr ay($get_user)) {
            > ---------------------------------------------------------------[/color]

            Badr, that's incorrect. What he had works correctly (on that line). The
            idea is to call mysql_fetch_arr ay, and put the return value in $data. If
            the return value == TRUE (or, !== FALSE) then continue in the loop. It's a
            shorthand way of saying:

            $data = mysql_fetch_arr ay($get_user);
            if($data)
            {
            //do stuff
            }
            [color=blue]
            > but i have to tell you about very practical method in your situation ,
            > i think if you have very large project or more than 5 PHP files you may
            > use the MySQL Class for connectivity and query purposes check this site
            > and you will find a lot of free MySQL Classes
            >
            > http://www.weberdev.com/get_example-3733.html (better one i think)
            > http://www.weberdev.com/get_example-1505.html
            > http://www.weberdev.com/get_example-1645.html
            >
            > tell me about the result.[/color]

            A class is one way to do it, but not really necessary. If you want other
            features (such as remembering queries, caching results, etc.) you may want
            to go a bit more in-depth. However, with what you need (just a start
            connection and end connection), you really don't need to make a class (or
            use any of the ones linked to by Badr - they aren't all that helpful).



            Comment

            • Chung Leong

              #7
              Re: Connect &amp; Disconnetcting functions

              "Sadara" <sadaraNOWAY@NO WAYdds.nl> wrote in message
              news:41d9a6bd$0 $6206$e4fe514c@ news.xs4all.nl. ..[color=blue]
              > knoak wrote:[color=green]
              > > Hi there,
              > >
              > > I'm building a DB-driven site, and i have one page (settings.php) that
              > > is included on each viewable page. In settings.php i have a few
              > > functions, so i do not have to type them all, or modify them all if
              > > anything changes..
              > >
              > > For the same reason i want to create a function to connect to a mySQL
              > > DB.
              > > What i have is:
              > >
              > > ############### ############### ############### ##
              > > function doConnect(){
              > > $db = mysql_connect(" host", "user", "pass");
              > > mysql_select_db ("data",$db) ;
              > > };
              > >
              > > function doDisConnect(){
              > > mysql_close($db );
              > > };
              > > ############### ############### ############### ##[/color]
              >
              > the value of the variable $db is most likely local to the function
              > doConnect() and will therefore not be available to the mysql_query call.
              > you could either make $db global in scope, or make its value the return
              > value of doConnect().[/color]

              Not "most likely." It IS local to the function.


              Comment

              • Sadara

                #8
                Re: Connect &amp; Disconnetcting functions

                Chung Leong wrote:[color=blue]
                > "Sadara" <sadaraNOWAY@NO WAYdds.nl> wrote in message
                > news:41d9a6bd$0 $6206$e4fe514c@ news.xs4all.nl. ..
                >[color=green]
                >>knoak wrote:
                >>[color=darkred]
                >>>Hi there,
                >>>
                >>>I'm building a DB-driven site, and i have one page (settings.php) that
                >>>is included on each viewable page. In settings.php i have a few
                >>>functions, so i do not have to type them all, or modify them all if
                >>>anything changes..
                >>>
                >>>For the same reason i want to create a function to connect to a mySQL
                >>>DB.
                >>>What i have is:
                >>>
                >>>############ ############### ############### #####
                >>>function doConnect(){
                >>>$db = mysql_connect(" host", "user", "pass");
                >>>mysql_select _db("data",$db) ;
                >>>};
                >>>
                >>>function doDisConnect(){
                >>>mysql_close( $db);
                >>>};
                >>>############ ############### ############### #####[/color]
                >>
                >>the value of the variable $db is most likely local to the function
                >>doConnect() and will therefore not be available to the mysql_query call.
                >>you could either make $db global in scope, or make its value the return
                >>value of doConnect().[/color]
                >
                >
                > Not "most likely." It IS local to the function.[/color]

                it's not possible to see from the code quoted whether the variable has
                been declared elsewhere with global scope.

                Comment

                • Jan Pieter Kunst

                  #9
                  Re: Connect &amp; Disconnetcting functions

                  Sadara wrote:[color=blue]
                  > Chung Leong wrote:
                  >[color=green]
                  >> "Sadara" <sadaraNOWAY@NO WAYdds.nl> wrote in message
                  >> news:41d9a6bd$0 $6206$e4fe514c@ news.xs4all.nl. ..
                  >>[color=darkred]
                  >>> knoak wrote:
                  >>>
                  >>>> Hi there,
                  >>>>
                  >>>> I'm building a DB-driven site, and i have one page (settings.php) that
                  >>>> is included on each viewable page. In settings.php i have a few
                  >>>> functions, so i do not have to type them all, or modify them all if
                  >>>> anything changes..
                  >>>>
                  >>>> For the same reason i want to create a function to connect to a mySQL
                  >>>> DB.
                  >>>> What i have is:
                  >>>>
                  >>>> ############### ############### ############### ##
                  >>>> function doConnect(){
                  >>>> $db = mysql_connect(" host", "user", "pass");
                  >>>> mysql_select_db ("data",$db) ;
                  >>>> };
                  >>>>
                  >>>> function doDisConnect(){
                  >>>> mysql_close($db );
                  >>>> };
                  >>>> ############### ############### ############### ##
                  >>>
                  >>>
                  >>> the value of the variable $db is most likely local to the function
                  >>> doConnect() and will therefore not be available to the mysql_query call.
                  >>> you could either make $db global in scope, or make its value the return
                  >>> value of doConnect().[/color]
                  >>
                  >>
                  >>
                  >> Not "most likely." It IS local to the function.[/color]
                  >
                  >
                  > it's not possible to see from the code quoted whether the variable has
                  > been declared elsewhere with global scope.[/color]

                  In that case the variable $db in the doConnect() function still has
                  nothing to do with the global $db. If you want global variables in your
                  functions you need to explicitly declare them as global in every
                  function where you want to use them:

                  $db = something;

                  function a() {
                  global $db;
                  ... $db ... // global $db
                  }

                  function b() {
                  global $db;
                  ... $db .... // global $db
                  }

                  function c() {
                  ... $db ... // local $db
                  }

                  JP

                  --
                  Sorry, <devnull@cauce. org> is a spam trap.
                  Real e-mail address unavailable. 5000+ spams per month.

                  Comment

                  • Sadara

                    #10
                    Re: Connect &amp; Disconnetcting functions

                    Jan Pieter Kunst wrote:[color=blue]
                    > Sadara wrote:
                    >[color=green]
                    >> Chung Leong wrote:
                    >>[color=darkred]
                    >>> "Sadara" <sadaraNOWAY@NO WAYdds.nl> wrote in message
                    >>> news:41d9a6bd$0 $6206$e4fe514c@ news.xs4all.nl. ..
                    >>>
                    >>>> knoak wrote:
                    >>>>
                    >>>>> Hi there,
                    >>>>>
                    >>>>> I'm building a DB-driven site, and i have one page (settings.php) that
                    >>>>> is included on each viewable page. In settings.php i have a few
                    >>>>> functions, so i do not have to type them all, or modify them all if
                    >>>>> anything changes..
                    >>>>>
                    >>>>> For the same reason i want to create a function to connect to a mySQL
                    >>>>> DB.
                    >>>>> What i have is:
                    >>>>>
                    >>>>> ############### ############### ############### ##
                    >>>>> function doConnect(){
                    >>>>> $db = mysql_connect(" host", "user", "pass");
                    >>>>> mysql_select_db ("data",$db) ;
                    >>>>> };
                    >>>>>
                    >>>>> function doDisConnect(){
                    >>>>> mysql_close($db );
                    >>>>> };
                    >>>>> ############### ############### ############### ##
                    >>>>
                    >>>>
                    >>>>
                    >>>> the value of the variable $db is most likely local to the function
                    >>>> doConnect() and will therefore not be available to the mysql_query
                    >>>> call.
                    >>>> you could either make $db global in scope, or make its value the return
                    >>>> value of doConnect().
                    >>>
                    >>>
                    >>>
                    >>>
                    >>> Not "most likely." It IS local to the function.[/color]
                    >>
                    >>
                    >>
                    >> it's not possible to see from the code quoted whether the variable has
                    >> been declared elsewhere with global scope.[/color]
                    >
                    >
                    > In that case the variable $db in the doConnect() function still has
                    > nothing to do with the global $db. If you want global variables in your
                    > functions you need to explicitly declare them as global in every
                    > function where you want to use them:
                    >
                    > $db = something;
                    >
                    > function a() {
                    > global $db;
                    > ... $db ... // global $db
                    > }
                    >
                    > function b() {
                    > global $db;
                    > ... $db .... // global $db
                    > }
                    >
                    > function c() {
                    > ... $db ... // local $db
                    > }
                    >
                    > JP
                    >[/color]

                    thanks for correcting me.

                    Comment

                    • iMedia

                      #11
                      Re: Connect &amp; Disconnetcting functions

                      I thought globals were taboo?

                      Comment

                      • Michael Fesser

                        #12
                        Re: Connect &amp; Disconnetcting functions

                        .oO(iMedia)
                        [color=blue]
                        >I thought globals were taboo?[/color]

                        Why?

                        Micha

                        Comment

                        • jblanch

                          #13
                          Re: Connect &amp; Disconnetcting functions

                          function dbConn(){
                          $db = @mysql_connect( "user","pass"," host");
                          if(!$db){
                          echo "Mysql Connect error";
                          exit;
                          }
                          if(!@mysql_sele ct_db("db_name" )){
                          echo "DB select Error";
                          exit;
                          }
                          return $db;
                          }

                          function dbQuery($query) {
                          // you can put many security checks into your code easily
                          // by making it a new function, along with easy error checking
                          $db = dbConn();
                          if($db){
                          $s =@ mysql_query($qu ery,$db);
                          if(!$s){
                          echo "Error on query";
                          exit;
                          }
                          return mysql_result($s );
                          }
                          }

                          this is the setup i use.. its quite easy and i find it faster to type
                          dbQuery instead of mysql_query.. plus this adds better security on
                          your query functions, and better error checking. you obiously should
                          add your own modifications to this code, like maybe line checking
                          (__LINE__) or file (__FILE__) or somthing.. but this causes the db
                          connection to be returned.

                          This might, probably does acctually.. make your connections keep
                          opening, so what you can do is just use the disconnect code like you
                          have after all querys are done.

                          Comment

                          • iMedia

                            #14
                            Re: Connect &amp; Disconnetcting functions

                            I wish I could say exactly. I'm fairly new to PHP. I read a few months
                            ago that globals are now considered insecure and bad practice. Is this
                            true?

                            Comment

                            • Michael Fesser

                              #15
                              Re: Connect &amp; Disconnetcting functions

                              .oO(iMedia)
                              [color=blue]
                              >I wish I could say exactly. I'm fairly new to PHP. I read a few months
                              >ago that globals are now considered insecure and bad practice. Is this
                              >true?[/color]

                              You're probably talking about register_global s, which indeed can become
                              a security risk if not used properly.

                              Micha

                              Comment

                              Working...