simple php question: mysql_fetch_row display array contents

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

    #1

    simple php question: mysql_fetch_row display array contents

    I am trying to do something in PHP that I thought would be fairly easy
    but has become rather troublesome.

    I'm trying to retrieve a list of numbers from a MySQL database and
    display the contents in a comma delimited string if there is more than
    one element. My problem is that the first element of the array is
    displaying but not any of the others and I can't figure out why.


    $count_lot = 0;
    while ($row = mysql_fetch_row ($sql_lot_num_r esult))
    {
    $lot_num_array[$count_lot] = $row[$count_lot];
    echo "<p>\$lot_num_a rray[$count_lot] is:
    $lot_num_array[$count_lot]<br />";
    echo "\$count_lo t is: $count_lot</p>";

    $count_lot++;
    }

    // if there is more than 1 element in the array then separate them
    with a comma and space
    if ($count_lot == 1)
    {
    $lot_num_string = "$lot_num_a rray[0]";
    }
    else
    {
    $lot_num_string = implode(", ", $lot_num_array) ;
    }

    echo "<p>\$lot_num_s tring is: $lot_num_string </p>";

    ---

    The result is:

    $lot_num_array[0] is: 0111
    $count_lot is: 0

    $lot_num_array[1] is:
    $count_lot is: 1

    $lot_num_array[2] is:
    $count_lot is: 2

    $lot_num_string is: 0111, ,

    ---

    Why is only the first element displaying but not the rest?

    Tony W.
    tonyw(no_spam)@ liquidationworl d.com
  • Jeffrey Silverman

    #2
    Re: simple php question: mysql_fetch_row display array contents

    On Mon, 18 Aug 2003 09:44:31 -0700, Tony W. wrote:
    [color=blue]
    > I am trying to do something in PHP that I thought would be fairly easy but
    > has become rather troublesome.[/color]
    <snip!>

    There are a bunch of potential problems with your code, but I am too beat
    right now to clarify. Sorry. I probably shouldn't even have responded at
    all. I know I would find it annoying if someone responded to *me* with
    this sort of answer...

    i'll post a real answer later...
    --
    Jeffrey D. Silverman | jeffrey AT jhu DOT edu
    Johns Hopkins University | Baltimore, MD
    Website | http://www.wse.jhu.edu/newtnotes/

    Comment

    • Tom Thackrey

      #3
      Re: simple php question: mysql_fetch_row display array contents


      On 18-Aug-2003, tonyw@liquidati onworld.com (Tony W.) wrote:
      [color=blue]
      > I'm trying to retrieve a list of numbers from a MySQL database and
      > display the contents in a comma delimited string if there is more than
      > one element. My problem is that the first element of the array is
      > displaying but not any of the others and I can't figure out why.
      >
      >
      > $count_lot = 0;
      > while ($row = mysql_fetch_row ($sql_lot_num_r esult))
      > {
      > $lot_num_array[$count_lot] = $row[$count_lot];[/color]

      The index to $row should be the column name or index, but not $count_lot
      [color=blue]
      > echo "<p>\$lot_num_a rray[$count_lot] is:
      > $lot_num_array[$count_lot]<br />";
      > echo "\$count_lo t is: $count_lot</p>";
      >
      > $count_lot++;
      > }
      >[/color]
      (snip)


      --
      Tom Thackrey

      Comment

      Working...