Strange mysql_fetch_array() behavior

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

    Strange mysql_fetch_array() behavior

    Hi,

    I have the following code:

    $resultSet = mysql_query($sq lString);
    $numResults = mysql_num_rows( $resultSet);
    echo "Num Results = " . $numResults . "<br/>";
    for ($i = 0; $i < $numResults; $i++) {
    $row = mysql_fetch_arr ay($resultSet);
    echo "i = " . $i . "<br/>";
    foreach ($row as $key=>$r) {
    echo $key . " = " . $r . "<br/>";
    }
    }

    When I run the query at the MySQL Command prompt, I get exactly one
    record, which is correct. However, when I echo the key value pairs,
    it is printing the values twice, once with a numeric key (index) and
    then again with the proper key name. For example, here is the first
    bit of the output:

    i = 0
    0 = 2
    ApptID = 2
    1 = 1
    CustID = 1
    2 = 1
    DogID = 1
    3 = 2
    4 = 12:00:00
    ApptTime = 12:00:00
    5 = 03:00:00
    PickupTime = 03:00:00
    6 = 1
    7 = Mac
    Name = Mac
    8 = 1
    9 = Kevin and Erika
    FName = Kevin and Erika
    10 = Holleran
    LName = Holleran

    I just want the key=>pairs! How do I get rid of the index thing?!

    Thanks for any help.

    Kevin
  • Jerry Stuckle

    #2
    Re: Strange mysql_fetch_arr ay() behavior

    KDawg44 wrote:
    Hi,
    >
    I have the following code:
    >
    $resultSet = mysql_query($sq lString);
    $numResults = mysql_num_rows( $resultSet);
    echo "Num Results = " . $numResults . "<br/>";
    for ($i = 0; $i < $numResults; $i++) {
    $row = mysql_fetch_arr ay($resultSet);
    echo "i = " . $i . "<br/>";
    foreach ($row as $key=>$r) {
    echo $key . " = " . $r . "<br/>";
    }
    }
    >
    When I run the query at the MySQL Command prompt, I get exactly one
    record, which is correct. However, when I echo the key value pairs,
    it is printing the values twice, once with a numeric key (index) and
    then again with the proper key name. For example, here is the first
    bit of the output:
    >
    i = 0
    0 = 2
    ApptID = 2
    1 = 1
    CustID = 1
    2 = 1
    DogID = 1
    3 = 2
    4 = 12:00:00
    ApptTime = 12:00:00
    5 = 03:00:00
    PickupTime = 03:00:00
    6 = 1
    7 = Mac
    Name = Mac
    8 = 1
    9 = Kevin and Erika
    FName = Kevin and Erika
    10 = Holleran
    LName = Holleran
    >
    I just want the key=>pairs! How do I get rid of the index thing?!
    >
    Thanks for any help.
    >
    Kevin
    >
    Fetch a result row as an associative array, a numeric array, or both



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

    Comment

    • KDawg44

      #3
      Re: Strange mysql_fetch_arr ay() behavior

      On Feb 9, 10:21 pm, Jerry Stuckle <jstuck...@attg lobal.netwrote:
      KDawg44 wrote:
      Hi,
      >
      I have the following code:
      >
      $resultSet = mysql_query($sq lString);
      $numResults = mysql_num_rows( $resultSet);
      echo "Num Results = " . $numResults . "<br/>";
      for ($i = 0; $i < $numResults; $i++) {
      $row = mysql_fetch_arr ay($resultSet);
      echo "i = " . $i . "<br/>";
      foreach ($row as $key=>$r) {
      echo $key . " = " . $r . "<br/>";
      }
      }
      >
      When I run the query at the MySQL Command prompt, I get exactly one
      record, which is correct. However, when I echo the key value pairs,
      it is printing the values twice, once with a numeric key (index) and
      then again with the proper key name. For example, here is the first
      bit of the output:
      >
      i = 0
      0 = 2
      ApptID = 2
      1 = 1
      CustID = 1
      2 = 1
      DogID = 1
      3 = 2
      4 = 12:00:00
      ApptTime = 12:00:00
      5 = 03:00:00
      PickupTime = 03:00:00
      6 = 1
      7 = Mac
      Name = Mac
      8 = 1
      9 = Kevin and Erika
      FName = Kevin and Erika
      10 = Holleran
      LName = Holleran
      >
      I just want the key=>pairs! How do I get rid of the index thing?!
      >
      Thanks for any help.
      >
      Kevin
      >
      Fetch a result row as an associative array, a numeric array, or both

      >
      --
      =============== ===
      Remove the "x" from my email address
      Jerry Stuckle
      JDS Computer Training Corp.
      jstuck...@attgl obal.net
      =============== ===
      By strange behavior i meant normal behavior.... thanks! I use a text
      as a reference (little older, maybe that's why) and it did not say
      anything about this.

      Thanks again Jerry! You are the king.

      Comment

      Working...