How can I get just one row from selected column?

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

    How can I get just one row from selected column?

    Hi.
    How can I get just one row from selected column and put it into html
    dropdown list
    I tried like this:

    function pobierz_wszystk o($tabela,$kolu mna)
    {
    $zapytanie="SEL ECT $kolumna FROM $tabela";
    $wynik=mysql_qu ery($zapytanie) ;
    while($wiersz=m ysql_fetch_arra y($wynik,MYSQL_ ASSOC))
    {
    echo "<option value=$wiersz>$ wiersz</option> <br />";
    }
    }

    <select name="$hname">
    <option value="*" selected >All</option>
    <?php
    pobierz_wszystk o('hotel','hote l_nazwa');
    ?>
    </select>


    But it doesn't work properly because I'm getting a dropdown list with
    All,and Array,Array,Arr ay,Array,Array
    Instead of Array I'd like to have a value from a row.

    Is it because I'm using mysql_fetch_arr ay($wynik,MYSQL _ASSOC)?
    or maybe the query is wrong?

    Thanks
    Leszek


  • Dikkie Dik

    #2
    Re: How can I get just one row from selected column?

    If you use mysql_fetch_arr ay($wynik,MYSQL _NUM)
    you can write $wiersz[0] to access the value.
    Also, if the value contains spaces, your option HTML will probably not
    work as expected.

    Leszek wrote:[color=blue]
    > Hi.
    > How can I get just one row from selected column and put it into html
    > dropdown list
    > I tried like this:
    >
    > function pobierz_wszystk o($tabela,$kolu mna)
    > {
    > $zapytanie="SEL ECT $kolumna FROM $tabela";
    > $wynik=mysql_qu ery($zapytanie) ;
    > while($wiersz=m ysql_fetch_arra y($wynik,MYSQL_ ASSOC))
    > {
    > echo "<option value=$wiersz>$ wiersz</option> <br />";
    > }
    > }
    >
    > <select name="$hname">
    > <option value="*" selected >All</option>
    > <?php
    > pobierz_wszystk o('hotel','hote l_nazwa');
    > ?>
    > </select>
    >
    >
    > But it doesn't work properly because I'm getting a dropdown list with
    > All,and Array,Array,Arr ay,Array,Array
    > Instead of Array I'd like to have a value from a row.
    >
    > Is it because I'm using mysql_fetch_arr ay($wynik,MYSQL _ASSOC)?
    > or maybe the query is wrong?
    >
    > Thanks
    > Leszek
    >
    >[/color]

    Comment

    • Jerry Stuckle

      #3
      Re: How can I get just one row from selected column?

      Leszek wrote:[color=blue]
      > Hi.
      > How can I get just one row from selected column and put it into html
      > dropdown list
      > I tried like this:
      >
      > function pobierz_wszystk o($tabela,$kolu mna)
      > {
      > $zapytanie="SEL ECT $kolumna FROM $tabela";
      > $wynik=mysql_qu ery($zapytanie) ;
      > while($wiersz=m ysql_fetch_arra y($wynik,MYSQL_ ASSOC))
      > {
      > echo "<option value=$wiersz>$ wiersz</option> <br />";
      > }
      > }
      >
      > <select name="$hname">
      > <option value="*" selected >All</option>
      > <?php
      > pobierz_wszystk o('hotel','hote l_nazwa');
      > ?>
      > </select>
      >
      >
      > But it doesn't work properly because I'm getting a dropdown list with
      > All,and Array,Array,Arr ay,Array,Array
      > Instead of Array I'd like to have a value from a row.
      >
      > Is it because I'm using mysql_fetch_arr ay($wynik,MYSQL _ASSOC)?
      > or maybe the query is wrong?
      >
      > Thanks
      > Leszek
      >
      >[/color]

      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

      • Iván Sánchez Ortega

        #4
        Re: How can I get just one row from selected column?

        -----BEGIN PGP SIGNED MESSAGE-----
        Hash: SHA1

        Leszek wrote:
        [color=blue]
        > while($wiersz=m ysql_fetch_arra y($wynik,MYSQL_ ASSOC))
        > {
        > echo "<option value=$wiersz>$ wiersz</option> <br />";
        > }[/color]

        Try something like

        $value = $wiersz[$kolumna];
        echo "<option value='$value'> $value</option> <br />";

        (remember to enclose the value parameter of the <option> tag in quotes, or
        your XHTML code will be invalid)

        - --
        - ----------------------------------
        Iván Sánchez Ortega -i-punto-sanchez--arroba-mirame-punto-net

        Un ordenador no es un televisor ni un microondas, es una herramienta
        compleja.
        -----BEGIN PGP SIGNATURE-----
        Version: GnuPG v1.4.2 (GNU/Linux)

        iD8DBQFDtBgq3jc Q2mg3Pc8RAlVWAJ 9iY0kHAQjAkLrG8 QStj2v3dLLKQACe KV5i
        7kYsmBjlAT92TX1 ejcCcpqw=
        =ehjI
        -----END PGP SIGNATURE-----

        Comment

        • Hilarion

          #5
          Re: How can I get just one row from selected column?

          >> while($wiersz=m ysql_fetch_arra y($wynik,MYSQL_ ASSOC))[color=blue][color=green]
          >> {
          >> echo "<option value=$wiersz>$ wiersz</option> <br />";
          >> }[/color]
          >
          > Try something like
          >
          > $value = $wiersz[$kolumna];
          > echo "<option value='$value'> $value</option> <br />";
          >
          > (remember to enclose the value parameter of the <option> tag in quotes, or
          > your XHTML code will be invalid)[/color]


          To make sure it's valid, you should also use "htmlspecialcha rs"
          function (if you use double-quotes around attribute values,
          then you do not have to specify the second parameter for this function,
          but if you use single-quotes - as in the example above - then you
          should specify that "htmlspecialcha rs" should also escape single-quotes).


          Hilarion

          Comment

          • Gazelem

            #6
            Re: How can I get just one row from selected column?

            Leszek wrote:[color=blue]
            > Hi.
            > How can I get just one row from selected column and put it into html
            > dropdown list
            > I tried like this:
            >
            > function pobierz_wszystk o($tabela,$kolu mna)
            > {
            > $zapytanie="SEL ECT $kolumna FROM $tabela";
            > $wynik=mysql_qu ery($zapytanie) ;
            > while($wiersz=m ysql_fetch_arra y($wynik,MYSQL_ ASSOC))
            > {
            > echo "<option value=$wiersz>$ wiersz</option> <br />";
            > }
            > }[/color]

            The query is wrong among other things.

            proper: Select $kolumna from $tabela limit 1

            You can also use an offset, reference the manual at


            Another thing is, if you are *always* going to want just one result, not
            only should you use the proper select, but you should also limit your code
            to only ask for 1 result:

            $wynik=mysql_qu ery($zapytanie) ;
            $wiersz=mysql_f etch_array($wyn ik,MYSQL_ASSOC)
            echo "<option value=$wiersz>$ wiersz</option> <br />";

            notice the lack of using a "while" statement, which is not appropriate for 1
            result queries.

            -Dave



            Comment

            • Hilarion

              #7
              Re: How can I get just one row from selected column?

              >> How can I get just one row from selected column and put it into html[color=blue][color=green]
              >> dropdown list
              >> I tried like this:
              >>
              >> function pobierz_wszystk o($tabela,$kolu mna)
              >> {
              >> $zapytanie="SEL ECT $kolumna FROM $tabela";
              >> $wynik=mysql_qu ery($zapytanie) ;
              >> while($wiersz=m ysql_fetch_arra y($wynik,MYSQL_ ASSOC))
              >> {
              >> echo "<option value=$wiersz>$ wiersz</option> <br />";
              >> }
              >> }[/color]
              >
              > The query is wrong among other things.
              >
              > proper: Select $kolumna from $tabela limit 1
              >
              > You can also use an offset, reference the manual at
              > http://dev.mysql.com/doc/refman/4.1/en/select.html
              >
              > Another thing is, if you are *always* going to want just one result, not
              > only should you use the proper select, but you should also limit your code
              > to only ask for 1 result:
              >
              > $wynik=mysql_qu ery($zapytanie) ;
              > $wiersz=mysql_f etch_array($wyn ik,MYSQL_ASSOC)
              > echo "<option value=$wiersz>$ wiersz</option> <br />";
              >
              > notice the lack of using a "while" statement, which is not appropriate for 1
              > result queries.[/color]


              I think that Leszek wanted to ask "how can I get just one COLUMN from...",
              in which case LIMIT clause will not be what he looks for.
              As others explained - Leszek used the PHP mysql functions output in
              a wrong way, and it had nothing to do with SQL syntax.


              Hilarion

              Comment

              • Jim Michaels

                #8
                Re: How can I get just one row from selected column?


                "Hilarion" <hilarion@SPAM. op.SMIECI.pl> wrote in message
                news:dpgdhj$t9k $1@news.onet.pl ...[color=blue][color=green][color=darkred]
                >>> How can I get just one row from selected column and put it into html
                >>> dropdown list
                >>> I tried like this:
                >>>
                >>> function pobierz_wszystk o($tabela,$kolu mna)
                >>> {
                >>> $zapytanie="SEL ECT $kolumna FROM $tabela";
                >>> $wynik=mysql_qu ery($zapytanie) ;
                >>> while($wiersz=m ysql_fetch_arra y($wynik,MYSQL_ ASSOC))
                >>> {
                >>> echo "<option value=$wiersz>$ wiersz</option> <br />";
                >>> }
                >>> }[/color]
                >>
                >> The query is wrong among other things.
                >>
                >> proper: Select $kolumna from $tabela limit 1
                >>
                >> You can also use an offset, reference the manual at
                >> http://dev.mysql.com/doc/refman/4.1/en/select.html
                >>
                >> Another thing is, if you are *always* going to want just one result, not
                >> only should you use the proper select, but you should also limit your
                >> code to only ask for 1 result:
                >>
                >> $wynik=mysql_qu ery($zapytanie) ;
                >> $wiersz=mysql_f etch_array($wyn ik,MYSQL_ASSOC)
                >> echo "<option value=$wiersz>$ wiersz</option> <br />";
                >>
                >> notice the lack of using a "while" statement, which is not appropriate
                >> for 1 result queries.[/color]
                >
                >
                > I think that Leszek wanted to ask "how can I get just one COLUMN from...",
                > in which case LIMIT clause will not be what he looks for.
                > As others explained - Leszek used the PHP mysql functions output in
                > a wrong way, and it had nothing to do with SQL syntax.[/color]


                Actually, he wanted "just one row from a selected column". If he was
                speaking about a *particular* row, I would suggest a WHERE clause (SELECT
                $kolumna FROM $tablea WHERE id=5), or a counter+if approach.
                I am not sure exactly what it is he is trying to do.. maybe trying to pick
                out a row to put a SELECTED attribute on the <option> on or something?
                (speak up!)
                $count=1;
                while($wiersz=m ysql_fetch_arra y($wynik,MYSQL_ ASSOC))
                {
                echo "<option value=$wiersz>$ wiersz</option> <br />";
                if (4==count) {
                do something here
                }
                $count++;
                }

                the problem with this is, how are you going to guarantee that there will
                always be more than 4 rows, if this is what he is really asking?

                And if he is wanting just the first row from a SELECT, yeah, a LIMIT 1 would
                be good to append on the statement. but I would suggest the following code
                below in case you get no rows (you can drop the else part if you want):
                He should remove the <br /> tag out of the <select></select> area - it
                should not be beside an <option> tag. it's illegal - it will really mess
                things up for the browser and you may get inconsistent cross-browser
                renderings. I think maybe what he was trying for was \n instead, which the
                browser ignores, but looks good when viewing code.
                $wynik=mysql_qu ery($zapytanie) ;
                if ($wiersz=mysql_ fetch_array($wy nik,MYSQL_ASSOC )) {
                echo "<option value=$wiersz>$ wiersz</option>\n";
                } else {
                echo "<!--no rows.-->";
                }

                [color=blue]
                >
                >
                > Hilarion[/color]


                Comment

                • Jim Michaels

                  #9
                  Re: How can I get just one row from selected column?


                  "Leszek" <leszekt80@pocz ta.onet.pl> wrote in message
                  news:dp0v53$b4r $1@news.onet.pl ...[color=blue]
                  > Hi.
                  > How can I get just one row from selected column and put it into html
                  > dropdown list
                  > I tried like this:
                  >
                  > function pobierz_wszystk o($tabela,$kolu mna)[/color]

                  you are referencing the array wrong. it should be $wiersz[$kolumna] when you
                  want to extract data from the column.
                  echo "<option value=$wiersz[$kolumna]>$wiersz[$kolumna]</option> <br
                  />";

                  [color=blue]
                  > {
                  > $zapytanie="SEL ECT $kolumna FROM $tabela";
                  > $wynik=mysql_qu ery($zapytanie) ;
                  > while($wiersz=m ysql_fetch_arra y($wynik,MYSQL_ ASSOC))
                  > {
                  > echo "<option value=$wiersz>$ wiersz</option> <br />";
                  > }
                  > }
                  >
                  > <select name="$hname">
                  > <option value="*" selected >All</option>
                  > <?php
                  > pobierz_wszystk o('hotel','hote l_nazwa');
                  > ?>
                  > </select>
                  >
                  >
                  > But it doesn't work properly because I'm getting a dropdown list with
                  > All,and Array,Array,Arr ay,Array,Array
                  > Instead of Array I'd like to have a value from a row.
                  >
                  > Is it because I'm using mysql_fetch_arr ay($wynik,MYSQL _ASSOC)?
                  > or maybe the query is wrong?
                  >
                  > Thanks
                  > Leszek
                  >
                  >[/color]


                  Comment

                  • Jim Michaels

                    #10
                    Re: How can I get just one row from selected column?


                    "Jim Michaels" <jmichae3@yahoo .com> wrote in message
                    news:sZqdnfGR7o ThFEDeRVn-vQ@comcast.com. ..[color=blue]
                    >
                    > "Hilarion" <hilarion@SPAM. op.SMIECI.pl> wrote in message
                    > news:dpgdhj$t9k $1@news.onet.pl ...[color=green][color=darkred]
                    >>>> How can I get just one row from selected column and put it into html
                    >>>> dropdown list
                    >>>> I tried like this:
                    >>>>
                    >>>> function pobierz_wszystk o($tabela,$kolu mna)
                    >>>> {
                    >>>> $zapytanie="SEL ECT $kolumna FROM $tabela";
                    >>>> $wynik=mysql_qu ery($zapytanie) ;
                    >>>> while($wiersz=m ysql_fetch_arra y($wynik,MYSQL_ ASSOC))
                    >>>> {
                    >>>> echo "<option value=$wiersz>$ wiersz</option> <br />";
                    >>>> }
                    >>>> }
                    >>>
                    >>> The query is wrong among other things.
                    >>>
                    >>> proper: Select $kolumna from $tabela limit 1
                    >>>
                    >>> You can also use an offset, reference the manual at
                    >>> http://dev.mysql.com/doc/refman/4.1/en/select.html
                    >>>
                    >>> Another thing is, if you are *always* going to want just one result, not
                    >>> only should you use the proper select, but you should also limit your
                    >>> code to only ask for 1 result:
                    >>>
                    >>> $wynik=mysql_qu ery($zapytanie) ;
                    >>> $wiersz=mysql_f etch_array($wyn ik,MYSQL_ASSOC)
                    >>> echo "<option value=$wiersz>$ wiersz</option> <br />";
                    >>>
                    >>> notice the lack of using a "while" statement, which is not appropriate
                    >>> for 1 result queries.[/color]
                    >>
                    >>
                    >> I think that Leszek wanted to ask "how can I get just one COLUMN
                    >> from...",
                    >> in which case LIMIT clause will not be what he looks for.
                    >> As others explained - Leszek used the PHP mysql functions output in
                    >> a wrong way, and it had nothing to do with SQL syntax.[/color][/color]


                    OOPS! code fix. array referenced wrong. didn't catch this until a later
                    post. fixed below.
                    [color=blue]
                    >
                    >
                    > Actually, he wanted "just one row from a selected column". If he was
                    > speaking about a *particular* row, I would suggest a WHERE clause (SELECT
                    > $kolumna FROM $tablea WHERE id=5), or a counter+if approach.
                    > I am not sure exactly what it is he is trying to do.. maybe trying to pick
                    > out a row to put a SELECTED attribute on the <option> on or something?
                    > (speak up!)[/color]
                    $count=1;
                    while($wiersz=m ysql_fetch_arra y($wynik,MYSQL_ ASSOC))
                    {
                    echo "<option value=$wiersz[$kolumna]>$wiersz[$kolumna]</option> <br
                    />";
                    if (4==count) {
                    do something here
                    }
                    $count++;
                    }[color=blue]
                    >
                    > the problem with this is, how are you going to guarantee that there will
                    > always be more than 4 rows, if this is what he is really asking?
                    >
                    > And if he is wanting just the first row from a SELECT, yeah, a LIMIT 1
                    > would be good to append on the statement. but I would suggest the
                    > following code below in case you get no rows (you can drop the else part
                    > if you want):
                    > He should remove the <br /> tag out of the <select></select> area - it
                    > should not be beside an <option> tag. it's illegal - it will really mess
                    > things up for the browser and you may get inconsistent cross-browser
                    > renderings. I think maybe what he was trying for was \n instead, which
                    > the browser ignores, but looks good when viewing code.[/color]
                    $wynik=mysql_qu ery($zapytanie) ;
                    if ($wiersz=mysql_ fetch_array($wy nik,MYSQL_ASSOC )) {
                    echo "<option value=$wiersz[$kolumna]>$wiersz[$kolumna]</option>\n";
                    } else {
                    echo "<!--no rows.-->";
                    }[color=blue]
                    >
                    >[color=green]
                    >>
                    >>
                    >> Hilarion[/color]
                    >
                    >[/color]


                    Comment

                    Working...