An extra last empty field in an 'mysql_fetch_array' result array?

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

    #1

    An extra last empty field in an 'mysql_fetch_array' result array?

    Hello,

    I seem to get an extra empty field in every 'mysql_fetch_ar ray' command I issue. For example:

    I have a simple table 'tblName':

    ID Name
    1 Jane
    2 Joe
    2 Doe

    The following code:

    $oCursor = mysql_query("SE LECT ID from tblName WHERE Name='Jane'");
    if (!$oCursor)
    {
    $bGo = false;
    }
    else
    {
    $aRow = mysql_fetch_arr ay($oCursor);
    }

    results in:

    count($aRow) = 2;

    $aRow[0] = 1;
    $aRow[1] = '';

    Am I missing something, doing something wrong, a wrong PHP setting?

    Thanks,
    Marcel Brekelmans

  • windandwaves

    #2
    Re: An extra last empty field in an 'mysql_fetch_ar ray' result array?


    "Marcel Brekelmans" <marcel@marce l-art.com> wrote in message
    news:29udnWsDMp JDW9PenZ2dnUVZ8 qKdnZ2d@giganew s.com...
    .....[color=blue]
    >$oCursor = mysql_query("SE LECT ID from tblName WHERE Name='Jane'");
    >if (!$oCursor)
    >{
    > $bGo = false;
    >}
    >else
    >{
    > $aRow = mysql_fetch_arr ay($oCursor);
    >}[/color]
    [color=blue]
    >results in:[/color]
    [color=blue]
    >count($aRow) = 2;[/color]
    [color=blue]
    >$aRow[0] = 1;
    >$aRow[1] = '';[/color]
    [color=blue]
    >Am I missing something, doing something wrong, a wrong PHP setting?[/color]

    Hoi Marcel

    This is the function I use:

    function getdata($sql) {
    $result = @mysql_query($s ql) or die("Error: " . mysql_error()." sql was
    ".$sql);
    $ret = array();
    while($row = mysql_fetch_ass oc($result)) {
    $ret[] = $row;
    }
    mysql_free_resu lt($result);
    return $ret;
    }

    then, in my code, I used:

    $sql = "SELECT ID from tblName WHERE Name='Jane'";
    $data = getdata($sql)
    foreach ($data as $ds) {
    echo $ds["Name"];
    }

    Once you use it a few times, it just makes life really easy, especially
    because you refer to fields by name rather than position (e.g.$ds[0]).

    Having said that, I am not sure what you are doing wrong ;-) I think that
    you only fetch one row. A better way to do this is

    while($row = mysql_fetch_row ($query)) {

    }

    Note the single "is teken".

    - Nicolaas


    Comment

    • Peter van Schie

      #3
      Re: An extra last empty field in an 'mysql_fetch_ar ray' result array?

      Marcel Brekelmans wrote:[color=blue]
      > The following code:
      >
      > $oCursor = mysql_query("SE LECT ID from tblName WHERE Name='Jane'");
      > if (!$oCursor)
      > {
      > $bGo = false;
      > }
      > else
      > {
      > $aRow = mysql_fetch_arr ay($oCursor);
      > }
      >
      > results in:
      >
      > count($aRow) = 2;
      >
      > $aRow[0] = 1;
      > $aRow[1] = '';[/color]

      Marcel,

      mysql_fetch_arr ay by default fetches the result both as an associative
      array and a numeric array. It has two parameters, the second parameter
      is either MYSQL_ASSOC, MYSQL_NUM or MYSQL_BOTH, default is MYSQL_BOTH.
      Doing a print_r($aRow) will show this too.

      HTH.
      Peter.
      --

      Comment

      • brekelm

        #4
        Re: An extra last empty field in an 'mysql_fetch_ar ray' result array?


        Peter van Schie schreef:
        [color=blue]
        > Marcel Brekelmans wrote:[color=green]
        > > The following code:
        > >
        > > $oCursor = mysql_query("SE LECT ID from tblName WHERE Name='Jane'");
        > > if (!$oCursor)
        > > {
        > > $bGo = false;
        > > }
        > > else
        > > {
        > > $aRow = mysql_fetch_arr ay($oCursor);
        > > }
        > >
        > > results in:
        > >
        > > count($aRow) = 2;
        > >
        > > $aRow[0] = 1;
        > > $aRow[1] = '';[/color]
        >
        > Marcel,
        >
        > mysql_fetch_arr ay by default fetches the result both as an associative
        > array and a numeric array. It has two parameters, the second parameter
        > is either MYSQL_ASSOC, MYSQL_NUM or MYSQL_BOTH, default is MYSQL_BOTH.
        > Doing a print_r($aRow) will show this too.
        >
        > HTH.
        > Peter.
        > --
        > http://www.phpforums.nl[/color]

        Thanks Peter, that was the solution: using the MYSQL_NUM parameter
        restricted the output to the single value I expected.

        Comment

        • Justin Koivisto

          #5
          Re: An extra last empty field in an 'mysql_fetch_ar ray' result array?

          Please... post in plain text & do it only once (there are 6 identical
          messages)

          Marcel Brekelmans wrote:
          [color=blue]
          > I seem to get an extra empty field in every 'mysql_fetch_ar ray' command
          > I issue. For example:
          >
          > I have a simple table 'tblName':
          >
          > ID Name
          > 1 Jane
          > 2 Joe
          > 2 Doe
          >
          > The following code:
          >
          > $oCursor = mysql_query("SE LECT ID from tblName WHERE Name='Jane'");
          > if (!$oCursor)
          > {
          > $bGo = false;
          > }
          > else
          > {
          > $aRow = mysql_fetch_arr ay($oCursor);
          > }
          >
          > results in:
          >
          > count($aRow) = 2;
          >
          > $aRow[0] = 1;
          > $aRow[1] = '';
          >
          > Am I missing something, doing something wrong, a wrong PHP setting?[/color]

          What is actually set is the following:

          Array
          (
          [0] => 1
          [ID] => 1
          )

          That is because mysql_fetch_arr ay by default returns by column name and
          by number...

          Use mysql_fetch_arr ay($oCursor,MYS QL_NUM) or
          mysql_fetch_arr ay($oCursor,MYS QL_ASSOC) instead.

          Fetch a result row as an associative array, a numeric array, or both


          (see "Return Values" section)

          --
          Justin Koivisto, ZCE - justin@koivi.co m

          Comment

          Working...