Question about Null values

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

    Question about Null values

    A section of the documentation for the isSet() function states:

    Also note that a NULL byte ("\0") is not equivalent to the PHP NULL
    constant.

    Where would you encounter a NULL byte? Is a null byte what is returned
    from a field in a database?

    Josh
  • André Næss

    #2
    Re: Question about Null values

    Joshua Ruppert:
    [color=blue]
    > A section of the documentation for the isSet() function states:
    >
    > Also note that a NULL byte ("\0") is not equivalent to the PHP NULL
    > constant.
    >
    > Where would you encounter a NULL byte? Is a null byte what is returned
    > from a field in a database?[/color]

    It's typically used in C to mark the end of a string. As you might know the
    programmer is responsible for managing memory in C. This means that if you
    want to store a string coming from the user using C, you will have to set
    aside some buffer space, say 4096B. If the user sends a string of only 67B
    the \0 byte is used in this buffer to mark the end of the string.

    Luckily it's not anything you really have to worry about in PHP.

    André Næss

    Comment

    • Christian Fersch

      #3
      Re: Re: Question about Null values

      > It's typically used in C to mark the end of a string. As you might know the[color=blue]
      > programmer is responsible for managing memory in C. This means that if you
      > want to store a string coming from the user using C, you will have to set
      > aside some buffer space, say 4096B. If the user sends a string of only 67B
      > the \0 byte is used in this buffer to mark the end of the string.
      >
      > Luckily it's not anything you really have to worry about in PHP.[/color]

      right - in php, the NULL Character is a Character like every other (besindes some functions don't work right with it)

      But nothing should give you such a Character (besindes the evil evil user)


      --
      mfg Christian (Chronial "at" web.de)

      --
      Composed with Newz Crawler 1.5 http://www.newzcrawler.com/

      Comment

      • David Mackenzie

        #4
        Re: Question about Null values

        On Tue, 16 Dec 2003 12:03:31 +0100, "Christian Fersch"
        <Fraghunter@web .de> wrote:
        [color=blue][color=green]
        >> It's typically used in C to mark the end of a string. As you might know the
        >> programmer is responsible for managing memory in C. This means that if you
        >> want to store a string coming from the user using C, you will have to set
        >> aside some buffer space, say 4096B. If the user sends a string of only 67B
        >> the \0 byte is used in this buffer to mark the end of the string.
        >>
        >> Luckily it's not anything you really have to worry about in PHP.[/color]
        >
        >right - in php, the NULL Character is a Character like every other (besindes some functions don't work right with it)
        >
        >But nothing should give you such a Character (besindes the evil evil user)[/color]

        If you were to retrieve a BLOB field from a database, it could contain
        "null" (\0) characters.

        In database terms, "NULL" means "no data". The "null character" *is*
        data - a byte with a value of zero.

        --
        David ( @priz.co.uk )
        The Internet Prisoner Database: http://www.priz.co.uk/ipdb/
        The Tarbrax Chronicle: http://www.tarbraxchronicle.com/

        Comment

        • Allodoxaphobia

          #5
          Re: Question about Null values

          On Tue, 16 Dec 2003 11:18:55 +0000, David Mackenzie hath writ:
          [color=blue]
          > ....... The "null character" *is* data - a byte with a value of zero.[/color]

          More properly: "...a byte with a value of" *nothing* .
          Not zero , not alpha , not numeric , not feet , not inches...
          At best it is a placeholder -- where _something_ could be -- but,
          at the present time, there is *nothing* there.

          As a thing meaning *nothing*, in C-like constructs it is usually
          used the show the end of *something* .

          Jonesy
          --
          | Marvin L Jones | jonz | W3DHJ | OS/2
          | Gunnison, Colorado | @ | Jonesy | linux __
          | 7,703' -- 2,345m | config.com | DM68mn SK

          Comment

          • Andy Hassall

            #6
            Re: Question about Null values

            On 16 Dec 2003 16:12:12 GMT, Allodoxaphobia <bit-bucket@config.c om> wrote:
            [color=blue]
            >On Tue, 16 Dec 2003 11:18:55 +0000, David Mackenzie hath writ:
            >[color=green]
            >> ....... The "null character" *is* data - a byte with a value of zero.[/color]
            >
            >More properly: "...a byte with a value of" *nothing* .
            >Not zero , not alpha , not numeric , not feet , not inches...
            >At best it is a placeholder -- where _something_ could be -- but,
            >at the present time, there is *nothing* there.[/color]

            Well, a NUL character is CHR(0) and is a one-byte zero, it is very much
            something. You seem to be almost describing an SQL database NULL, which
            represents 'unknown'.
            [color=blue]
            >As a thing meaning *nothing*, in C-like constructs it is usually
            >used the show the end of *something* .[/color]

            Character strings are terminated in C by a NUL (chr(0)) character. It is
            definitely 'something' itself, since you have to allocate space for it. PHP's a
            higher-level language so you don't have to worry about NUL terminators. And you
            can have NUL bytes in PHP 'string' variables, as they're actually
            'binary-safe'.

            <pre>
            <?php
            $x = chr(0).chr(0).c hr(0).chr(0).'x ';
            var_dump($x);
            ?>
            </pre>

            string(5) "x"

            (Note the length!)

            --
            Andy Hassall (andy@andyh.co. uk) icq(5747695) (http://www.andyh.co.uk)
            Space: disk usage analysis tool (http://www.andyhsoftware.co.uk/space)

            Comment

            • Joshua Ruppert

              #7
              Re: Question about Null values

              So would a null byte be basically an empty string as opposed to an
              unknown value?

              Josh

              Andy Hassall wrote:
              [color=blue]
              > On 16 Dec 2003 16:12:12 GMT, Allodoxaphobia <bit-bucket@config.c om> wrote:
              >
              >[color=green]
              >>On Tue, 16 Dec 2003 11:18:55 +0000, David Mackenzie hath writ:
              >>
              >>[color=darkred]
              >>>....... The "null character" *is* data - a byte with a value of zero.[/color]
              >>
              >>More properly: "...a byte with a value of" *nothing* .
              >>Not zero , not alpha , not numeric , not feet , not inches...
              >>At best it is a placeholder -- where _something_ could be -- but,
              >>at the present time, there is *nothing* there.[/color]
              >
              >
              > Well, a NUL character is CHR(0) and is a one-byte zero, it is very much
              > something. You seem to be almost describing an SQL database NULL, which
              > represents 'unknown'.
              >
              >[color=green]
              >>As a thing meaning *nothing*, in C-like constructs it is usually
              >>used the show the end of *something* .[/color]
              >
              >
              > Character strings are terminated in C by a NUL (chr(0)) character. It is
              > definitely 'something' itself, since you have to allocate space for it. PHP's a
              > higher-level language so you don't have to worry about NUL terminators. And you
              > can have NUL bytes in PHP 'string' variables, as they're actually
              > 'binary-safe'.
              >
              > <pre>
              > <?php
              > $x = chr(0).chr(0).c hr(0).chr(0).'x ';
              > var_dump($x);
              > ?>
              > </pre>
              >
              > string(5) "x"
              >
              > (Note the length!)
              >[/color]

              Comment

              • Andy Hassall

                #8
                Re: Question about Null values

                On Tue, 16 Dec 2003 23:04:36 GMT, Joshua Ruppert
                <noSpamjruppert @rochester.rr.c om> wrote:
                [color=blue]
                >So would a null byte be basically an empty string as opposed to an
                >unknown value?[/color]

                In which language?

                C: Yes, a single NUL character is the representation of a zero-length string.
                PHP: No, it'd be a 1-character string consisting of a chr(0).

                --
                Andy Hassall (andy@andyh.co. uk) icq(5747695) (http://www.andyh.co.uk)
                Space: disk usage analysis tool (http://www.andyhsoftware.co.uk/space)

                Comment

                • Adam M. Johnson

                  #9
                  Re: Question about Null values

                  Another Newbie here...

                  doing a small order tracking app to learn PHP, MySQL, etc...

                  With regard to null values, I have a customer entry form in html.Some
                  fields are optional. I send data to MySQL via a POST method. When I
                  try to query the database later (via a form as well) and use "IS
                  NULL", for example:

                  select c.lname, c.fname
                  from Customer c
                  where c.company_name is null

                  ....I get no results. I have to query the db with something like

                  select c.lname, c.fname
                  from Customer c
                  where (c.company_name ='')

                  What am I doing wrong? There do not seem to be any null values in my
                  db, only empty values ('').

                  I suspect it has to do with the addslashes() function applied to the
                  post variables on the customer insert page. Must I NOT addslashes() if
                  the POST field has been skipped by the user?

                  Thanks!



                  On Tue, 16 Dec 2003 23:25:55 +0000, Andy Hassall <andy@andyh.co. uk>
                  wrote:
                  [color=blue]
                  >On Tue, 16 Dec 2003 23:04:36 GMT, Joshua Ruppert
                  ><noSpamjrupper t@rochester.rr. com> wrote:
                  >[color=green]
                  >>So would a null byte be basically an empty string as opposed to an
                  >>unknown value?[/color]
                  >
                  > In which language?
                  >
                  > C: Yes, a single NUL character is the representation of a zero-length string.
                  > PHP: No, it'd be a 1-character string consisting of a chr(0).[/color]

                  Comment

                  • steven mestdagh

                    #10
                    Re: Question about Null values

                    Adam M. Johnson <adamj@althealt h.org> wrote:[color=blue]
                    > With regard to null values, I have a customer entry form in html.Some
                    > fields are optional. I send data to MySQL via a POST method. When I
                    > try to query the database later (via a form as well) and use "IS
                    > NULL", for example:
                    >
                    > select c.lname, c.fname
                    > from Customer c
                    > where c.company_name is null
                    >
                    > ...I get no results. I have to query the db with something like
                    >
                    > select c.lname, c.fname
                    > from Customer c
                    > where (c.company_name ='')
                    >
                    > What am I doing wrong? There do not seem to be any null values in my
                    > db, only empty values ('').
                    >
                    > I suspect it has to do with the addslashes() function applied to the
                    > post variables on the customer insert page. Must I NOT addslashes() if
                    > the POST field has been skipped by the user?[/color]

                    perhaps you can test your POST variables before inserting them in the
                    database, i.e.

                    if (empty($_POST['myvar'])) {
                    $myvar = 'NULL';
                    }
                    else {
                    // note the surrounding quotes
                    $myvar = "'".mysql_escap e_string(strips lashes($_POST['myvar']))."'";
                    }

                    mysql_query("UP DATE table SET field=".$myvar) ;
                    (or INSERT, whatever you are using...)

                    if they are empty, NULL will be inserted in the database; if not,
                    stripslashes strips \ from the variable the form transmits, and
                    mysql_escape_st ring escapes special characters when passing the sql query
                    statement.

                    Comment

                    Working...