Foreach displaying data twice

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

    Foreach displaying data twice

    I must be missing something rather obvious. I have the following
    snippet of code that echo's my result twice when it should be echoing
    just once (only one element in the array). What am I overlooking?


    *************** *************** **
    $OpenQ = mysql_query("SE LECT column FROM table WHERE q_uid =
    $_SESSION[userid] AND status = 1",$db); // SQL query on DB returns 1
    result

    $Qresult = mysql_fetch_arr ay($OpenQ); // Fetch the array

    if ($_GET['OQ']) { // If the get variable is recieved
    foreach ($Qresult as $Q) {
    echo "<br>" . $Q;
    }
    }

    *************** *************** ***

    TY.

  • Rami Elomaa

    #2
    Re: Foreach displaying data twice

    Akhenaten kirjoitti:
    I must be missing something rather obvious. I have the following
    snippet of code that echo's my result twice when it should be echoing
    just once (only one element in the array). What am I overlooking?
    >
    >
    *************** *************** **
    $OpenQ = mysql_query("SE LECT column FROM table WHERE q_uid =
    $_SESSION[userid] AND status = 1",$db); // SQL query on DB returns 1
    result
    >
    $Qresult = mysql_fetch_arr ay($OpenQ); // Fetch the array
    This returns an array that has both index and associative keys. Ie. what
    you get is array(0=>'somet hing','column'= >'something') ;
    >
    if ($_GET['OQ']) { // If the get variable is recieved
    foreach ($Qresult as $Q) {
    When you loop thru the array, naturally you'll go thru both the index
    and the associative key, which both point to the same value. Instead of
    foreaching, you could just echo $Qresult[0] or use mysql_fetch_row or
    mysql_fetch_ass oc.
    echo "<br>" . $Q;
    }
    }
    >
    *************** *************** ***
    >
    TY.
    >

    --
    Rami.Elomaa@gma il.com

    "Wikipedia on vähän niinq internetin raamattu, kukaan ei pohjimmiltaan
    usko siihen ja kukaan ei tiedä mikä pitää paikkansa." -- z00ze

    Comment

    • Schraalhans Keukenmeester

      #3
      Re: Foreach displaying data twice

      At Fri, 01 Jun 2007 07:59:11 -0700, Akhenaten let h(is|er) monkeys type:
      I must be missing something rather obvious. I have the following
      snippet of code that echo's my result twice when it should be echoing
      just once (only one element in the array). What am I overlooking?
      >
      >
      *************** *************** **
      $OpenQ = mysql_query("SE LECT column FROM table WHERE q_uid =
      $_SESSION[userid] AND status = 1",$db); // SQL query on DB returns 1
      result
      Rami has answered your question. I just wondered whether:
      a) userid is a defined constant. If not, it should be in quotes.
      b) $_SESSION[userid] (and q_uid) is a numerical or a string. If the
      latter, it should be in quotes.

      Just my 2cts.

      Sh.

      Comment

      • Rami Elomaa

        #4
        Re: Foreach displaying data twice

        Schraalhans Keukenmeester kirjoitti:
        At Fri, 01 Jun 2007 07:59:11 -0700, Akhenaten let h(is|er) monkeys type:
        >
        >I must be missing something rather obvious. I have the following
        >snippet of code that echo's my result twice when it should be echoing
        >just once (only one element in the array). What am I overlooking?
        >>
        >>
        >************** *************** ***
        >$OpenQ = mysql_query("SE LECT column FROM table WHERE q_uid =
        >$_SESSION[userid] AND status = 1",$db); // SQL query on DB returns 1
        >result
        >
        Rami has answered your question. I just wondered whether:
        a) userid is a defined constant. If not, it should be in quotes.
        I used to think this too, but after reading the manual once again
        carefully, I discovered that this is not the case. Singlequotes in array
        keys aren't required when inside double quotes. It will not look for the
        constant, _unless_ you also use curly braces around it.

        $foo = "$array[key]"; // This is right, assumes key is a string

        $foo = "{$array[key]}"; // this assumes key is a constant, then falls
        back to string, so it'll trigger some stupid notice..

        $foo = "{$array['key']}"; // This again is right

        $foo = "$array['key']"; // quotes without curly braces is wrong, parse
        error.

        The thing is, if you're calling a multidimensiona l array, you gotta use
        the curly braces, and that forces you to use the single quotes:
        $foo = "{$array[0]['key']}";

        It's all here in the better book:


        --
        Rami.Elomaa@gma il.com

        "Wikipedia on vähän niinq internetin raamattu, kukaan ei pohjimmiltaan
        usko siihen ja kukaan ei tiedä mikä pitää paikkansa." -- z00ze

        Comment

        • Geoff Berrow

          #5
          Upload script failing

          I have an upload script which I want to use to upload word documents.
          This works fine on my Gradwell account. However on the client's
          (windows) hosting it fails.

          C:\WINDOWS\TEMP \php29D.tmp
          Warning: copy(papers/1180718015test. doc) [function.copy]: failed to open
          stream: Permission denied in
          \\nas01\nas\i\s \isbe2007.org\w eb\uploaddoc.ph p on line 22

          I emailed support (Namesco) and received this reply
          >There is no need to change permissions on Windows hosting. Your files by
          >default have the permissions to write to directories owned by your own
          >user.
          Any suggestions?

          --
          Regards,

          Geoff Berrow

          Comment

          • Schraalhans Keukenmeester

            #6
            Re: Foreach displaying data twice

            At Fri, 01 Jun 2007 19:29:46 +0300, Rami Elomaa let h(is|er) monkeys type:
            Schraalhans Keukenmeester kirjoitti:
            >>************* *************** ****
            >>$OpenQ = mysql_query("SE LECT column FROM table WHERE q_uid =
            >>$_SESSION[userid] AND status = 1",$db); // SQL query on DB returns 1
            >>result
            >>
            >Rami has answered your question. I just wondered whether:
            >a) userid is a defined constant. If not, it should be in quotes.
            >
            I used to think this too, but after reading the manual once again
            carefully, I discovered that this is not the case. Singlequotes in array
            keys aren't required when inside double quotes. It will not look for the
            constant, _unless_ you also use curly braces around it.
            >
            $foo = "$array[key]"; // This is right, assumes key is a string
            >
            $foo = "{$array[key]}"; // this assumes key is a constant, then falls
            back to string, so it'll trigger some stupid notice..
            >
            $foo = "{$array['key']}"; // This again is right
            >
            $foo = "$array['key']"; // quotes without curly braces is wrong, parse
            error.
            >
            The thing is, if you're calling a multidimensiona l array, you gotta use
            the curly braces, and that forces you to use the single quotes:
            $foo = "{$array[0]['key']}";
            >
            It's all here in the better book:
            http://fi.php.net/manual/en/language.types.string.php
            Thanks for setting me straight Rami! Just checked and found it works the
            other way around as well: if you DO use single quotes around an array
            index inside a double-quoted string curly's are required, lest php throws
            an error.
            $arr = array('foo'=>'b ar');
            echo "I met my friends in the $arr[foo]";
            I met my friends in the bar
            echo "I met my friends in the $arr['foo']";
            Parse error: syntax error, unexpected T_ENCAPSED_AND_ WHITESPACE,
            expecting T_STRING or T_VARIABLE or T_NUM_STRING
            echo "I met my friends in the {$arr['foo']}";
            I met my friends in the bar

            SOmehow I never thought about the reason why I had to em-brace
            one-dimensional array references inside "strings" or heredoc, which also
            expands variables of course.

            I think I prefer consistent use of curly braces in these situations, and
            have the same syntax with any kind of array. Personal taste & habit I
            guess.

            Never too old to learn.
            Rgds

            --
            Schraalhans Keukenmeester - schraalhans@the .Spamtrapexampl e.nl
            [Remove the lowercase part of Spamtrap to send me a message]

            "strcmp('apples ','oranges') < 0"

            Comment

            • Jerry Stuckle

              #7
              Re: Upload script failing

              Geoff Berrow wrote:
              I have an upload script which I want to use to upload word documents.
              This works fine on my Gradwell account. However on the client's
              (windows) hosting it fails.
              >
              C:\WINDOWS\TEMP \php29D.tmp
              Warning: copy(papers/1180718015test. doc) [function.copy]: failed to open
              stream: Permission denied in
              \\nas01\nas\i\s \isbe2007.org\w eb\uploaddoc.ph p on line 22
              >
              I emailed support (Namesco) and received this reply
              >
              >>There is no need to change permissions on Windows hosting. Your files by
              >>default have the permissions to write to directories owned by your own
              >>user.
              >
              Any suggestions?
              >
              They don't have any idea what they're talking about? You may be able to
              write to them as your user - but you need to be able to write them them
              as the web server's user. Completely separate requirements.

              --
              =============== ===
              Remove the "x" from my email address
              Jerry Stuckle
              JDS Computer Training Corp.
              jstucklex@attgl obal.net
              =============== ===

              Comment

              • Geoff Berrow

                #8
                Re: Upload script failing

                Message-ID: <wtidndyaLInZV_ 3bnZ2dnUVZWhedn Z2d@comcast.com from Jerry
                Stuckle contained the following:
                >Any suggestions?
                >>
                >
                >They don't have any idea what they're talking about? You may be able to
                >write to them as your user - but you need to be able to write them them
                >as the web server's user. Completely separate requirements.
                Thanks Jerry, I suspected I was being fobbed off and told them so.

                An equally unhelpful reply followed:-

                "Dear customer,

                The script might work elsewhere, but that is on a different server with
                a different configuration. We will not assist you with troubleshooting
                your scripts. You will need to establish the cause of the problem
                yourself and resolve it accordingly. "

                Perhaps they just don't want the business?


                --
                Geoff Berrow (put thecat out to email)
                It's only Usenet, no one dies.
                My opinions, not the committee's, mine.
                Simple RFDs http://www.ckdog.co.uk/rfdmaker/

                Comment

                • tomb

                  #9
                  Re: Upload script failing

                  Try www.technetcenter.com - they aren't perfect, but they are very
                  helpful and the hosting packages are really great.

                  T

                  Geoff Berrow wrote:
                  Message-ID: <wtidndyaLInZV_ 3bnZ2dnUVZWhedn Z2d@comcast.com from Jerry
                  Stuckle contained the following:
                  >
                  >>Any suggestions?
                  >>>
                  >They don't have any idea what they're talking about? You may be able to
                  >write to them as your user - but you need to be able to write them them
                  >as the web server's user. Completely separate requirements.
                  >
                  Thanks Jerry, I suspected I was being fobbed off and told them so.
                  >
                  An equally unhelpful reply followed:-
                  >
                  "Dear customer,
                  >
                  The script might work elsewhere, but that is on a different server with
                  a different configuration. We will not assist you with troubleshooting
                  your scripts. You will need to establish the cause of the problem
                  yourself and resolve it accordingly. "
                  >
                  Perhaps they just don't want the business?
                  >
                  >

                  Comment

                  • Jerry Stuckle

                    #10
                    Re: Upload script failing

                    Geoff Berrow wrote:
                    Message-ID: <wtidndyaLInZV_ 3bnZ2dnUVZWhedn Z2d@comcast.com from Jerry
                    Stuckle contained the following:
                    >
                    >>Any suggestions?
                    >>>
                    >They don't have any idea what they're talking about? You may be able to
                    >write to them as your user - but you need to be able to write them them
                    >as the web server's user. Completely separate requirements.
                    >
                    Thanks Jerry, I suspected I was being fobbed off and told them so.
                    >
                    An equally unhelpful reply followed:-
                    >
                    "Dear customer,
                    >
                    The script might work elsewhere, but that is on a different server with
                    a different configuration. We will not assist you with troubleshooting
                    your scripts. You will need to establish the cause of the problem
                    yourself and resolve it accordingly. "
                    >
                    Perhaps they just don't want the business?
                    >
                    >
                    It sure looks like it. I'd write them back and tell them that since
                    they can't configure their servers properly and don't even want to know
                    they have a problem, you're moving to another hosting company.

                    And then I'd go to www.webhostingtalk.com and let people know about this
                    company (including name). We don't need any more incompetent, uncaring
                    hosting companies.

                    --
                    =============== ===
                    Remove the "x" from my email address
                    Jerry Stuckle
                    JDS Computer Training Corp.
                    jstucklex@attgl obal.net
                    =============== ===

                    Comment

                    • Geoff Berrow

                      #11
                      Re: Upload script failing

                      Message-ID: <MMc8i.17859$92 3.2184@bignews3 .bellsouth.netf rom tomb
                      contained the following:
                      >Try www.technetcenter.com - they aren't perfect, but they are very
                      >helpful and the hosting packages are really great.
                      Changing to a more helpful host is probably the best idea but it's not
                      really an option at the moment.
                      --
                      Geoff Berrow (put thecat out to email)
                      It's only Usenet, no one dies.
                      My opinions, not the committee's, mine.
                      Simple RFDs http://www.ckdog.co.uk/rfdmaker/

                      Comment

                      • Jerry Stuckle

                        #12
                        Re: Upload script failing

                        Geoff Berrow wrote:
                        Message-ID: <MMc8i.17859$92 3.2184@bignews3 .bellsouth.netf rom tomb
                        contained the following:
                        >
                        >Try www.technetcenter.com - they aren't perfect, but they are very
                        >helpful and the hosting packages are really great.
                        >
                        Changing to a more helpful host is probably the best idea but it's not
                        really an option at the moment.
                        Changing hosts is *always* an option.

                        --
                        =============== ===
                        Remove the "x" from my email address
                        Jerry Stuckle
                        JDS Computer Training Corp.
                        jstucklex@attgl obal.net
                        =============== ===

                        Comment

                        • Schraalhans Keukenmeester

                          #13
                          Re: Upload script failing

                          At Sat, 02 Jun 2007 10:57:13 +0100, Geoff Berrow let h(is|er) monkeys
                          type:
                          Message-ID: <wtidndyaLInZV_ 3bnZ2dnUVZWhedn Z2d@comcast.com from Jerry
                          Stuckle contained the following:
                          >
                          >>Any suggestions?
                          >>>
                          Thanks Jerry, I suspected I was being fobbed off and told them so.
                          >
                          An equally unhelpful reply followed:-
                          >
                          "Dear customer,
                          >
                          The script might work elsewhere, but that is on a different server with
                          a different configuration. We will not assist you with troubleshooting
                          your scripts. You will need to establish the cause of the problem
                          yourself and resolve it accordingly. "
                          >
                          Aha, the typical "Not my problem" approach. Goes hand in hand with "We
                          don't make any mistakes. Your fault". Change hosts. ASAP. Waste of money &
                          energy. And tell them why you leave. And make sure you tell everyone about
                          this utterly poor service. Take your losses, and if they sold you a
                          product including service, just don't pay for what they don't deliver.

                          Good luck.
                          --
                          Schraalhans Keukenmeester - schraalhans@the .Spamtrapexampl e.nl
                          [Remove the lowercase part of Spamtrap to send me a message]

                          "strcmp('apples ','oranges') < 0"

                          Comment

                          • Rami Elomaa

                            #14
                            Re: Upload script failing

                            Geoff Berrow kirjoitti:
                            I have an upload script which I want to use to upload word documents.
                            This works fine on my Gradwell account. However on the client's
                            (windows) hosting it fails.
                            >
                            C:\WINDOWS\TEMP \php29D.tmp
                            Warning: copy(papers/1180718015test. doc) [function.copy]: failed to open
                            stream: Permission denied in
                            \\nas01\nas\i\s \isbe2007.org\w eb\uploaddoc.ph p on line 22
                            >
                            I emailed support (Namesco) and received this reply
                            >
                            >>There is no need to change permissions on Windows hosting. Your files by
                            >>default have the permissions to write to directories owned by your own
                            >>user.
                            >
                            Any suggestions?
                            >
                            Did you try move_uploaded_f ile() instead of copy()? If the host has
                            configured php to safe mode, this might make a difference.

                            It also might be that the host company are wankers who don't know their
                            heads from their asses, but the basic thing is that when you handle
                            uploaded files, you should always use move_uploaded_f ile() instead of
                            copy, and it's worth testing at least.

                            --
                            Rami.Elomaa@gma il.com

                            "Wikipedia on vähän niinq internetin raamattu, kukaan ei pohjimmiltaan
                            usko siihen ja kukaan ei tiedä mikä pitää paikkansa." -- z00ze

                            Comment

                            • Geoff Berrow

                              #15
                              Re: Upload script failing

                              Message-ID: <x42dncdK-Mkh_PzbnZ2dnUVZ _gudnZ2d@comcas t.comfrom Jerry
                              Stuckle contained the following:
                              >Changing to a more helpful host is probably the best idea but it's not
                              >really an option at the moment.
                              >
                              >Changing hosts is *always* an option.
                              It may come to that but the possible disruption would not be welcome at
                              the moment as the site is in active use.

                              --
                              Geoff Berrow (put thecat out to email)
                              It's only Usenet, no one dies.
                              My opinions, not the committee's, mine.
                              Simple RFDs http://www.ckdog.co.uk/rfdmaker/

                              Comment

                              Working...