Test for NULL values in array/sql database?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • madsgormlarsen@gmail.com

    Test for NULL values in array/sql database?

    Hi

    I need to test a field(colum) in a SQL database for for NULL values,
    and have done so in this way.

    $query = "SELECT j FROM Andersen";
    $result = mysql_query($qu ery, $link_id);
    $query_data = mysql_fetch_arr ay($result);
    if (array_sum($que ry_data)==0)
    {
    echo "er nul";
    }




    Does "mysql_fetch_ar ray($result)" change the NULL values to zero
    values? And is there a better way of doing it.

  • Ivan Omelchenko 608308824

    #2
    Re: Test for NULL values in array/sql database?

    madsgormlarsen@ gmail.com ÐÉÛÅÔ:[color=blue]
    > Hi
    >
    > I need to test a field(colum) in a SQL database for for NULL values,
    > and have done so in this way.
    >
    > $query = "SELECT j FROM Andersen";
    > $result = mysql_query($qu ery, $link_id);
    > $query_data = mysql_fetch_arr ay($result);
    > if (array_sum($que ry_data)==0)
    > {
    > echo "er nul";
    > }
    >
    >
    >
    >
    > Does "mysql_fetch_ar ray($result)" change the NULL values to zero
    > values? And is there a better way of doing it.
    >[/color]
    i guess you have to use === instead

    Comment

    • BearItAll

      #3
      Re: Test for NULL values in array/sql database?

      On Tue, 21 Jun 2005 00:03:26 -0700, madsgormlarsen wrote:
      [color=blue]
      > Hi
      >
      > I need to test a field(colum) in a SQL database for for NULL values, and
      > have done so in this way.
      >
      > $query = "SELECT j FROM Andersen";
      > $result = mysql_query($qu ery, $link_id); $query_data =
      > mysql_fetch_arr ay($result); if (array_sum($que ry_data)==0)
      > {
      > echo "er nul";
      > }
      > }
      >
      >
      >
      > Does "mysql_fetch_ar ray($result)" change the NULL values to zero values?
      > And is there a better way of doing it.[/color]

      It isn't really wise to assume NULL is equal to zero if they is a
      possibility of your sql/script being ported to another database.

      NULL is best thought of as an object rather than a value, so in your sql
      you would test using 'is'.

      Ex,

      $query = "SELECT j FROM Andersen WHERE column IS NULL";


      Comment

      • Ewoud Dronkert

        #4
        Re: Test for NULL values in array/sql database?

        On 21 Jun 2005 00:03:26 -0700, madsgormlarsen@ gmail.com wrote:[color=blue]
        > I need to test a field(colum) in a SQL database for for NULL values,
        > and have done so in this way.
        >
        > $query = "SELECT j FROM Andersen";
        > $result = mysql_query($qu ery, $link_id);
        > $query_data = mysql_fetch_arr ay($result);
        > if (array_sum($que ry_data)==0)
        > {
        > echo "er nul";
        > }[/color]

        Do you expect or want them all to be null, or some, or none?


        --
        Firefox Web Browser - Rediscover the web - http://getffox.com/
        Thunderbird E-mail and Newsgroups - http://gettbird.com/

        Comment

        • madsgormlarsen@gmail.com

          #5
          Re: Test for NULL values in array/sql database?

          Hi

          Thanks for the replies, I should have given some more information. I
          need to take data from a sql database and generate html tables on the
          basis of those data. But sometimes there will be columns that only
          contain NULL values, and in such an instance I do not want to draw that
          column in the html table. So therefore I need a function that will
          filter the data, so it does not contain columns with only NULL values.
          At this point my one guess would be that one could take all the
          information into a array, with mysql_fetch_arr ay and then filter the
          array, and draw the html table on the basis of the filtered array.

          I have been looking at different database to sql scrips around the net,
          but normally they draw one row at a time, meaning that I would be
          getting columns containing no information.

          If you know of a good script for the purpose that I can alter for my
          specific needs, then it would also be great.

          Quote >Ex, $query = "SELECT j FROM Andersen WHERE column IS NULL";

          Perhaps I could do something like

          $query = "SELECT * FROM Andersen WHERE column IS NULL";
          for ($i = 0, $ii = mysql_num_field s($result); $i < $ii; $i++) {
          $out .= mysql_field_nam e($result, $i)
          }

          To find all the empty columns, and the filter based on that.

          Thanks for all the help you have offered, and for any further tips.

          Comment

          • Ewoud Dronkert

            #6
            Re: Test for NULL values in array/sql database?

            On 21 Jun 2005 05:59:09 -0700, madsgormlarsen@ gmail.com wrote:[color=blue]
            > So therefore I need a function that will filter the data, so it
            > does not contain columns with only NULL values. [...]
            >
            > $query = "SELECT * FROM Andersen WHERE column IS NULL";[/color]

            Rather: $query = "SELECT * FROM Andersen WHERE column IS NOT NULL";



            --
            Firefox Web Browser - Rediscover the web - http://getffox.com/
            Thunderbird E-mail and Newsgroups - http://gettbird.com/

            Comment

            • madsgormlarsen@gmail.com

              #7
              Re: Test for NULL values in array/sql database?

              This is probaly a stupid question but in[color=blue]
              >$query = "SELECT * FROM Andersen WHERE column IS NOT NULL";[/color]

              Can you really write "column", because is does not seem possible to me,
              would I need to write the name of all the colums?

              Thanks for the suggestion, it vould really be the solution if I could
              get it to work.

              Comment

              • Ewoud Dronkert

                #8
                Re: Test for NULL values in array/sql database?

                On 21 Jun 2005 10:55:58 -0700, madsgormlarsen@ gmail.com wrote:[color=blue]
                > Can you really write "column", because is does not seem possible to me,
                > would I need to write the name of all the colums?[/color]

                Oh right now I understand: you want to skip records where all of the
                columns are NULL. The fact that there are such records seems very odd.


                --
                Firefox Web Browser - Rediscover the web - http://getffox.com/
                Thunderbird E-mail and Newsgroups - http://gettbird.com/

                Comment

                • madsgormlarsen@gmail.com

                  #9
                  Re: Test for NULL values in array/sql database?

                  Hi

                  Well there is about 250 products in the table, and then it should
                  become possible to choose for example two, and get all data on these
                  two, and for these specific two products there will at times be columns
                  with only NULL values.

                  I treid to write names of colums, and to but an * but it did not work,
                  got a mysql error.

                  ..Thanks anyway.

                  Comment

                  • Malcolm Dew-Jones

                    #10
                    Re: Test for NULL values in array/sql database?

                    madsgormlarsen@ gmail.com wrote:
                    : Hi

                    : I need to test a field(colum) in a SQL database for for NULL values,
                    : and have done so in this way.

                    : $query = "SELECT j FROM Andersen";
                    : $result = mysql_query($qu ery, $link_id);
                    : $query_data = mysql_fetch_arr ay($result);
                    : if (array_sum($que ry_data)==0)
                    : {
                    : echo "er nul";
                    : }


                    : Does "mysql_fetch_ar ray($result)" change the NULL values to zero
                    : values? And is there a better way of doing it.

                    I would try isset() on the array value, or compare the value to php's NULL
                    (check the manual for the correct syntax for php comparison using php's
                    NULL).



                    --

                    This space not for rent.

                    Comment

                    Working...