foreach

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

    foreach

    Hi
    What's the problem here:

    odbc_fetch_into ($rs, $row, &$arr);
    foreach ($arr as $elem) {
    echo $elem;
    echo " ";
    }

    I get the error:

    Warning: Invalid argument supplied for foreach() in
    C:\xampp\htdocs \Abschlussproje kt\auswertung.p hp on line 15

    altough I copied this from a book.
    Thanks and regards
    Michael
  • Andy Hassall

    #2
    Re: foreach

    On 12 Jul 2004 16:03:53 -0700, sgier@dplanet.c h (Michael) wrote:
    [color=blue]
    >What's the problem here:
    >
    >odbc_fetch_int o($rs, $row, &$arr);
    >foreach ($arr as $elem) {
    >echo $elem;
    >echo " ";
    >}
    >
    >I get the error:
    >
    >Warning: Invalid argument supplied for foreach() in
    >C:\xampp\htdoc s\Abschlussproj ekt\auswertung. php on line 15[/color]

    $arr isn't an array.
    [color=blue]
    >altough I copied this from a book.[/color]

    Does the book mention error checking and what happens when you run out of
    rows?

    --
    Andy Hassall <andy@andyh.co. uk> / Space: disk usage analysis tool
    http://www.andyh.co.uk / http://www.andyhsoftware.co.uk/space

    Comment

    • Michael

      #3
      Re: foreach

      Hi
      Nope...it doesn't seem to be very usable though. Well but why this is not
      working? Could you post the correct syntax please.
      Thanks and regards from Switzerland
      Michael

      $sql = "SELECT Name FROM kunden";
      $rs=odbc_exec($ con, $sql); #the connection is ok
      $row=1;
      odbc_fetch_into ($rs, $row, &$arr);
      foreach ($row as $arr) {
      echo $arr;
      echo " "; #I get only one Name
      }

      Comment

      • Alvaro G Vicario

        #4
        Re: foreach

        *** Michael wrote/escribió (13 Jul 2004 01:36:01 -0700):[color=blue]
        > odbc_fetch_into ($rs, $row, &$arr);
        > foreach ($row as $arr) {
        > echo $arr;
        > echo " "; #I get only one Name
        > }[/color]

        foreach($arr as $row){
        }

        --
        --
        -- Álvaro G. Vicario - Burgos, Spain
        --

        Comment

        • Michael

          #5
          Re: foreach

          No this doesn't work neither. I believe now this to be a bug. I tried
          20 examples from php.net and now I'm angry.
          my question now is how to show all records this way:

          odbc_fetch_row( $rs, $row);
          $num=odbc_num_r ows($rs);
          for ($col=1;$col<$n um+1;$col++) {
          echo odbc_result($rs , $col);
          echo " ";
          }

          Acutally I only get one row....Thanks indeed.
          Regards Michael

          Comment

          • Geoff Berrow

            #6
            Re: foreach

            I noticed that Message-ID:
            <ca15f395.04071 31235.21671a29@ posting.google. com> from Michael contained
            the following:
            [color=blue]
            >
            >odbc_fetch_row ($rs, $row);
            >$num=odbc_num_ rows($rs);
            >for ($col=1;$col<$n um+1;$col++) {
            >echo odbc_result($rs , $col);
            >echo " ";
            >}
            >
            >Acutally I only get one row....Thanks indeed.
            >Regards Michael[/color]

            That's because odbc_num_rows may not work properly with Select
            statements. Crazy, huh?
            See http://uk2.php.net/manual/en/function.odbc-num-rows.php

            Here's what I use

            $odbc=odbc_conn ect ('data_source_n ame', '', '') or die( "Could Not
            Connect to ODBC Database!" );

            $sql="SELECT * from table";
            $result=odbc_ex ec($odbc,$sql);
            while($myrow=od bc_fetch_array( $result)){
            print "Field1 = ".$myrow['field1']."<br>";
            print "Field2 = ".$myrow['field2']."<br>";
            print "Fieldn = ".$myrow['fieldn']."<br>";
            }

            Hope this helps. If you need the number of rows just stick a counter in
            the loop.


            --
            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

            • Alvaro G Vicario

              #7
              Re: foreach

              *** Michael wrote/escribió (13 Jul 2004 13:35:55 -0700):[color=blue]
              > No this doesn't work neither. I believe now this to be a bug.[/color]

              Use this and find out what your variables contain. It'll be faster than
              merely copying book examples and making them fit in your code:

              echo gettype($myvari able);
              echo '<pre>'; print_r($myvari able); echo '</pre>';

              --
              --
              -- Álvaro G. Vicario - Burgos, Spain
              --

              Comment

              • Roland Dirler

                #8
                Re: foreach

                Michael schrieb:
                < ...[color=blue]
                > odbc_fetch_row( $rs, $row);
                > $num=odbc_num_r ows($rs);
                > for ($col=1;$col<$n um+1;$col++) {
                > echo odbc_result($rs , $col);
                > echo " ";
                > }
                >
                > Acutally I only get one row....Thanks indeed.
                > ...[/color]

                That's right. You can get only one field in one row with odbc_result().
                It returns the specified FIELD of the actual ROW. If the field value is
                zero or greater than the number of fields an error occures. At least
                you have not to count rows (odbc_num_rows( )) to get the number of
                columns. Use odbc_num_fields () instead.
                I seem it's not the badest idea to drop an eye (or two ;-) into the
                function reference at the php documentation.

                Regards from Hessen
                Roland

                Comment

                Working...