first even php code

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

    first even php code

    Hi gurus

    This is my first ever, ever PHP script. I have called the page tt.php. Can
    someone tell me if I have done it right? I have a My SQL database with a
    table called messages with fields name and description.

    Thank you.

    - Nicolaas

    <?php


    function tt($vname){
    include_once("c onnectDB.php");
    $sql = "SELECT * FROM messages WHERE Name='$vname'";
    $result = mysql_query($sq l);
    $myrow = mysql_fetch_arr ay($result);
    $message = $myrow['Description'];
    //Close the database connection
    mysql_close();
    return $message

    }
    ?>


  • kicken

    #2
    Re: first even php code

    WindAndWaves wrote:[color=blue]
    > Hi gurus
    >
    > This is my first ever, ever PHP script. I have called the page tt.php. Can
    > someone tell me if I have done it right? I have a My SQL database with a
    > table called messages with fields name and description.
    >
    > Thank you.
    >
    > - Nicolaas
    >
    > <?php
    >
    >
    > function tt($vname){
    > include_once("c onnectDB.php");
    > $sql = "SELECT * FROM messages WHERE Name='$vname'";
    > $result = mysql_query($sq l);
    > $myrow = mysql_fetch_arr ay($result);
    > $message = $myrow['Description'];
    > //Close the database connection
    > mysql_close();
    > return $message
    >
    > }
    > ?>
    >
    >[/color]

    Looks ok to me syntax wise. That alone won't do anything though as you
    have to have some code outside the function which will call the tt
    function with some name as the argument.
    Example:

    tt('WindAndWave s');

    Also, usually it's better to select the file names, and not select *.
    In your case you have such a small table it won't hurt, but if you
    select * and only use a couple fields, you're wasting memory by pulling
    unneccesary fields. Like here you only use the Description field, so
    doing "SELECT Description FROM message WHERE Name='$vname'" would be better.

    Lastly, your include_once call should probably be outside the function's
    scope, likewise you should move the mysql_close call outside the
    function. Multiple calls to this function will likely not work because
    the first call would close the connection to the database, and the later
    calles would not reopen it and thus produce errors.

    So, syntatically, it seems fine. Logically, needs some work.

    <?php

    include_once("c onnectDB.php"); // I assume this file does mysql_connect
    // and mysql_select_db .

    function tt($vname){
    $sql = "SELECT Description FROM messages WHERE Name='$vname'";
    $result = mysql_query($sq l);
    $myrow = mysql_fetch_arr ay($result);
    $message = $myrow['Description'];
    return $message

    }

    echo tt('WindAndWave s');
    echo '<br />'.tt('kicken') ;

    //Close the database connection
    mysql_close();
    ?>

    Comment

    • WindAndWaves

      #3
      Re: first even php code


      "kicken" <slick@aoeex.co m> wrote in message
      news:MO6dnREKVt S6ukLcRVn-oQ@bresnan.com. ..[color=blue]
      > WindAndWaves wrote:[color=green]
      > > Hi gurus
      > >
      > > This is my first ever, ever PHP script. I have called the page tt.php.[/color][/color]
      Can[color=blue][color=green]
      > > someone tell me if I have done it right? I have a My SQL database with[/color][/color]
      a[color=blue][color=green]
      > > table called messages with fields name and description.
      > >
      > > Thank you.
      > >
      > > - Nicolaas
      > >
      > > <?php
      > >
      > >
      > > function tt($vname){
      > > include_once("c onnectDB.php");
      > > $sql = "SELECT * FROM messages WHERE Name='$vname'";
      > > $result = mysql_query($sq l);
      > > $myrow = mysql_fetch_arr ay($result);
      > > $message = $myrow['Description'];
      > > //Close the database connection
      > > mysql_close();
      > > return $message
      > >
      > > }
      > > ?>
      > >
      > >[/color]
      >
      > Looks ok to me syntax wise. That alone won't do anything though as you
      > have to have some code outside the function which will call the tt
      > function with some name as the argument.
      > Example:
      >
      > tt('WindAndWave s');
      >
      > Also, usually it's better to select the file names, and not select *.
      > In your case you have such a small table it won't hurt, but if you
      > select * and only use a couple fields, you're wasting memory by pulling
      > unneccesary fields. Like here you only use the Description field, so
      > doing "SELECT Description FROM message WHERE Name='$vname'" would be[/color]
      better.[color=blue]
      >
      > Lastly, your include_once call should probably be outside the function's
      > scope, likewise you should move the mysql_close call outside the
      > function. Multiple calls to this function will likely not work because
      > the first call would close the connection to the database, and the later
      > calles would not reopen it and thus produce errors.
      >
      > So, syntatically, it seems fine. Logically, needs some work.
      >
      > <?php
      >
      > include_once("c onnectDB.php"); // I assume this file does mysql_connect
      > // and mysql_select_db .
      >
      > function tt($vname){
      > $sql = "SELECT Description FROM messages WHERE Name='$vname'";
      > $result = mysql_query($sq l);
      > $myrow = mysql_fetch_arr ay($result);
      > $message = $myrow['Description'];
      > return $message
      >
      > }
      >
      > echo tt('WindAndWave s');
      > echo '<br />'.tt('kicken') ;
      >
      > //Close the database connection
      > mysql_close();
      > ?>[/color]

      Thank you for your reply. What I perhaps should have mentioned is that I
      wanted to use it in another php file to add text bits and pieces. In that
      way I can keep the code short in the other php files. I hope that makes
      sense. The way I get it in the other php file would be as follows:

      include_once("t t.php");

      $msg = tt('kicken');

      is this correct?



      Thanks again



      - Nicolaas






      Comment

      • kicken

        #4
        Re: first even php code

        WindAndWaves wrote:[color=blue]
        > "kicken" <slick@aoeex.co m> wrote in message
        > news:MO6dnREKVt S6ukLcRVn-oQ@bresnan.com. ..
        >[color=green]
        >>[snip][/color]
        >[color=green]
        >>Lastly, your include_once call should probably be outside the function's
        >>scope, likewise you should move the mysql_close call outside the
        >>function. Multiple calls to this function will likely not work because
        >>the first call would close the connection to the database, and the later
        >>calles would not reopen it and thus produce errors.
        >>
        >>So, syntatically, it seems fine. Logically, needs some work.
        >>
        >><?php
        >>
        >>include_once( "connectDB.php" ); // I assume this file does mysql_connect
        >>// and mysql_select_db .
        >>
        >>function tt($vname){
        >> $sql = "SELECT Description FROM messages WHERE Name='$vname'";
        >> $result = mysql_query($sq l);
        >> $myrow = mysql_fetch_arr ay($result);
        >> $message = $myrow['Description'];
        >> return $message
        >>
        >>}
        >>
        >>echo tt('WindAndWave s');
        >>echo '<br />'.tt('kicken') ;
        >>
        >>//Close the database connection
        >>mysql_close() ;
        >>?>[/color]
        >
        >
        > Thank you for your reply. What I perhaps should have mentioned is that I
        > wanted to use it in another php file to add text bits and pieces. In that
        > way I can keep the code short in the other php files. I hope that makes
        > sense. The way I get it in the other php file would be as follows:
        >
        > include_once("t t.php");
        >
        > $msg = tt('kicken');
        >
        > is this correct?
        >[/color]

        That will work fine, however my comment about the including of
        connectDB.php and the placement of mysql_close is still something that
        you need to consider if you plan to call the tt function multiple times.

        include_once('c onnectDB.php');
        include_once('t t.php');
        $msg = tt('kicken');
        mysql_close();

        Comment

        • Chung Leong

          #5
          Re: first even php code

          "WindAndWav es" <access@ngaru.c om> wrote in message
          news:ueFDd.5803 $mo2.399708@new s.xtra.co.nz...[color=blue]
          > Hi gurus
          >
          > This is my first ever, ever PHP script. I have called the page tt.php.[/color]
          Can[color=blue]
          > someone tell me if I have done it right? I have a My SQL database with a
          > table called messages with fields name and description.
          >
          > Thank you.
          >
          > - Nicolaas
          >
          > <?php
          >
          >
          > function tt($vname){
          > include_once("c onnectDB.php");
          > $sql = "SELECT * FROM messages WHERE Name='$vname'";
          > $result = mysql_query($sq l);
          > $myrow = mysql_fetch_arr ay($result);
          > $message = $myrow['Description'];
          > //Close the database connection
          > mysql_close();
          > return $message
          >
          > }
          > ?>[/color]

          Using include/require statements in lieu of a function call is a bad
          practice. Something shouldn't happen just because you include a file. The
          following is better:

          function tt($vname){
          connectDB();
          $sql = "SELECT * FROM messages WHERE Name='$vname'";
          $result = mysql_query($sq l);
          $myrow = mysql_fetch_arr ay($result);
          $message = $myrow['Description'];
          return $message
          }

          Where connectDB() is a function defined earlier or in an include file. If
          there's no connection, it makes one. Otherwise it does nothing.



          Comment

          • WindAndWaves

            #6
            Re: first even php code


            "kicken" <slick@aoeex.co m> wrote in message
            news:PP6dnbltOb 3MMULcRVn-iw@bresnan.com. ..[color=blue]
            > WindAndWaves wrote:[color=green]
            > > "kicken" <slick@aoeex.co m> wrote in message
            > > news:MO6dnREKVt S6ukLcRVn-oQ@bresnan.com. ..
            > >[color=darkred]
            > >>[snip][/color]
            > >[color=darkred]
            > >>Lastly, your include_once call should probably be outside the function's
            > >>scope, likewise you should move the mysql_close call outside the
            > >>function. Multiple calls to this function will likely not work because
            > >>the first call would close the connection to the database, and the later
            > >>calles would not reopen it and thus produce errors.
            > >>
            > >>So, syntatically, it seems fine. Logically, needs some work.
            > >>
            > >><?php
            > >>
            > >>include_once( "connectDB.php" ); // I assume this file does mysql_connect
            > >>// and mysql_select_db .
            > >>
            > >>function tt($vname){
            > >> $sql = "SELECT Description FROM messages WHERE Name='$vname'";
            > >> $result = mysql_query($sq l);
            > >> $myrow = mysql_fetch_arr ay($result);
            > >> $message = $myrow['Description'];
            > >> return $message
            > >>
            > >>}
            > >>
            > >>echo tt('WindAndWave s');
            > >>echo '<br />'.tt('kicken') ;
            > >>
            > >>//Close the database connection
            > >>mysql_close() ;
            > >>?>[/color]
            > >
            > >
            > > Thank you for your reply. What I perhaps should have mentioned is that[/color][/color]
            I[color=blue][color=green]
            > > wanted to use it in another php file to add text bits and pieces. In[/color][/color]
            that[color=blue][color=green]
            > > way I can keep the code short in the other php files. I hope that makes
            > > sense. The way I get it in the other php file would be as follows:
            > >
            > > include_once("t t.php");
            > >
            > > $msg = tt('kicken');
            > >
            > > is this correct?
            > >[/color]
            >
            > That will work fine, however my comment about the including of
            > connectDB.php and the placement of mysql_close is still something that
            > you need to consider if you plan to call the tt function multiple times.
            >
            > include_once('c onnectDB.php');
            > include_once('t t.php');
            > $msg = tt('kicken');
            > mysql_close();[/color]

            I have tried to implement the tt function, but now, when I try to change the
            header, anywhere in the SQL, i get the following error:

            Cannot modify header information - headers already sent by (output started
            at .... tt.php:24) in ...email.php on line 204

            Can you explain me what I do wrong (remember I am a complete novice!)

            Thank you

            - Nicolaas


            Comment

            • kicken

              #7
              Re: first even php code

              WindAndWaves wrote:[color=blue]
              >
              > I have tried to implement the tt function, but now, when I try to change the
              > header, anywhere in the SQL, i get the following error:
              >
              > Cannot modify header information - headers already sent by (output started
              > at .... tt.php:24) in ...email.php on line 204
              >
              > Can you explain me what I do wrong (remember I am a complete novice!)
              >
              > Thank you
              >
              > - Nicolaas
              >
              >[/color]

              Whenever you use any kind of function in PHP which sends data to the
              browser (eg, echo, print, printf, readfile, ...) or have any data
              outside of the <?php and ?> tags (including spaces, newlines, etc) then
              you can no longer use any function which require modification to the
              request headers (eg, header(), setcookie(), session_start() ).

              The reason for this is that the header of the request must come prior to
              the body. Once you send data to the brower by some means, PHP much
              compile the header and send it, then send your output.

              What kind of headers are you trying to send, where are you trying to
              send them from? Make sure there's no output prior to the call to
              header. According to the error, your output began on line 24 of tt.php.
              Not sure how long your tt function is now, but I'm thinking maybe you
              just have something outside the ?> php tag. Perhaps an ending new line
              or such?

              Comment

              • WindAndWaves

                #8
                Re: first even php code


                "kicken" <slick@aoeex.co m> wrote in message
                news:tqCdnVGVN9 IeRXncRVn-oA@bresnan.com. ..[color=blue]
                > WindAndWaves wrote:[color=green]
                > >
                > > I have tried to implement the tt function, but now, when I try to change[/color][/color]
                the[color=blue][color=green]
                > > header, anywhere in the SQL, i get the following error:
                > >
                > > Cannot modify header information - headers already sent by (output[/color][/color]
                started[color=blue][color=green]
                > > at .... tt.php:24) in ...email.php on line 204
                > >
                > > Can you explain me what I do wrong (remember I am a complete novice!)
                > >
                > > Thank you
                > >
                > > - Nicolaas
                > >
                > >[/color]
                >
                > Whenever you use any kind of function in PHP which sends data to the
                > browser (eg, echo, print, printf, readfile, ...) or have any data
                > outside of the <?php and ?> tags (including spaces, newlines, etc) then
                > you can no longer use any function which require modification to the
                > request headers (eg, header(), setcookie(), session_start() ).
                >
                > The reason for this is that the header of the request must come prior to
                > the body. Once you send data to the brower by some means, PHP much
                > compile the header and send it, then send your output.
                >
                > What kind of headers are you trying to send, where are you trying to
                > send them from? Make sure there's no output prior to the call to
                > header. According to the error, your output began on line 24 of tt.php.
                > Not sure how long your tt function is now, but I'm thinking maybe you
                > just have something outside the ?> php tag. Perhaps an ending new line
                > or such?[/color]

                Hmm,

                What happens is this


                Main page::

                <? ..... tt ......headercha nge ....>
                with the tt page called and included once

                I changed it around to:

                <? ....... header change .... tt ......?>

                and now it works, but it leaves me pretty limited in terms of the use of tt,
                as all my pages change the headers several time.

                I also had some empty lines after the } character in my tt function, does
                that matter?

                TIA

                - Nicolaas



                Comment

                Working...