What have I done wrong when creating this array ?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jeddiki
    Contributor
    • Jan 2009
    • 290

    What have I done wrong when creating this array ?

    I an trying to create arrays from from mysql table and I thought
    that I had the code right but I must have done something wrong ...

    This is my code:

    Code:
    $earn = array();
    $comm = array();
    $sql = "SELECT earn, comm,  FROM main WHERE id = '$Db_prod' ORDER BY mday_no DESC ";
    $result  = mysql_query($sql)    or die("could not execute SELECT cb_main". mysql_error());  
    $num = mysql_num_rows($result);
    if ($num == 0 ) {
       $earn[] = 0;
       $comm[] = 0;
       }
    else {
           while( $row = mysql_fetch_assoc($result) ) {  
                $earn[] = $row['earn'];
                 $comm[] =  $row['comm'];   
                }
          }

    I have tried this code and I am getting a problem with it:

    When I display the resulting array with this:
    print_r($earn);

    I get:

    Array ( [0] => Array ( ) [1] => 34.01 [2] => 33.36 [3] => 33.36 [4] => 82.25 [5] => 82.25 )

    So I seem to have created a double array somehow

    And hence max($earn); just returns "Array" rather than the
    maximum value.

    What am I doing wrong ?


    .
  • Atli
    Recognized Expert Expert
    • Nov 2006
    • 5062

    #2
    Hey.

    I don't see anything in that code that would cause this. Unless the values returned by MySQL are somehow messed up?

    If you just print the values, rather than store them in arrays, what do you get?
    [code=php]$sql = "SELECT earn, comm, FROM main WHERE id = '$Db_prod' ORDER BY mday_no DESC ";
    $result = mysql_query($sq l) or die("could not execute SELECT cb_main". mysql_error());
    if (mysql_num_rows ($result) > 0 )
    {
    header('content-type: text/plain;');
    while( $row = mysql_fetch_ass oc($result) )
    {
    echo "Earn: {$row['earn']} - Comm: {$row['comm']}\n";
    }
    } [/code]

    Comment

    • jeddiki
      Contributor
      • Jan 2009
      • 290

      #3
      I'll try that and let you know, I have to go out for a few hours.

      thanks ...


      .

      Comment

      • jeddiki
        Contributor
        • Jan 2009
        • 290

        #4
        OK,
        I tried that.

        And I get a normal output.

        I added this to my code:


        Code:
        echo "Earn: {$row['earn']} <br>";
        and I get this output:

        Earn: 17.78

        So I do not see where this double depth array
        comes from !

        Comment

        • jeddiki
          Contributor
          • Jan 2009
          • 290

          #5
          Thanks for the suggestion

          That helped me to Fix it - :)




          .

          Comment

          • Atli
            Recognized Expert Expert
            • Nov 2006
            • 5062

            #6
            No problem. Glad I could help :)

            Out of curiosity, what was wrong?

            Comment

            Working...