fetch_array()

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

    fetch_array()

    I am trying to incorporate a random quote feature on my website. I have
    a database table called random_quote with two columns. Column 1 is
    MessageID and column 2 is Message. Message contains several sentences,
    and MessageID contains a number. I have 5 entries in the table. Here is
    the code I am using to query the database and write the quote to the
    screen:

    $random=rand(1, 5);
    $query="SELECT Message FROM random_quote WHERE MessageID=$rand om";
    $message = mysql_query($qu ery) or die(mysql_error ());
    echo $message;

    But it only returns "Resource #5." I had someone mention
    mysql_fetch_arr ay, but that seems like an unecessary way to go about
    it. To me, logically the code above should work. It should pull the
    message from the database and assign it to $message. And then write
    $message to the screen. So what's wrong with it and how can I fix it?

  • Andy Hassall

    #2
    Re: fetch_array()

    On 21 Dec 2006 14:58:23 -0800, "Jerim79" <mylek@hotmail. comwrote:
    >I am trying to incorporate a random quote feature on my website. I have
    >a database table called random_quote with two columns. Column 1 is
    >MessageID and column 2 is Message. Message contains several sentences,
    >and MessageID contains a number. I have 5 entries in the table. Here is
    >the code I am using to query the database and write the quote to the
    >screen:
    >
    >$random=rand(1 ,5);
    >$query="SELE CT Message FROM random_quote WHERE MessageID=$rand om";
    >$message = mysql_query($qu ery) or die(mysql_error ());
    >echo $message;
    >
    >But it only returns "Resource #5." I had someone mention
    >mysql_fetch_ar ray,
    Yes.
    but that seems like an unecessary way to go about
    >it. To me, logically the code above should work. It should pull the
    >message from the database and assign it to $message. And then write
    >$message to the screen.
    No.
    >So what's wrong with it and how can I fix it?
    You've posted your own answer.

    --
    Andy Hassall :: andy@andyh.co.u k :: http://www.andyh.co.uk
    http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool

    Comment

    • Geoff Berrow

      #3
      Re: fetch_array()

      Message-ID: <1166741903.577 897.67580@80g20 00cwy.googlegro ups.comfrom
      Jerim79 contained the following:
      >$random=rand(1 ,5);
      >$query="SELE CT Message FROM random_quote WHERE MessageID=$rand om";
      >$message = mysql_query($qu ery) or die(mysql_error ());
      >echo $message;
      >
      >But it only returns "Resource #5." I had someone mention
      >mysql_fetch_ar ray, but that seems like an unecessary way to go about
      >it. To me, logically the code above should work.
      No, it returns a resource, as you have found out. The output you
      require may just be one thing, but a query can return many rows and
      columns of data. It needs further processing before you can do anything
      useful with it. You might only be interested in the number of rows
      where you could use mysql_num_rows( )

      But in your case, since you know that MessageID is unique you know it
      will only return one row.

      $random=rand(1, 5);
      $query="SELECT Message FROM random_quote WHERE MessageID=$rand om";
      $result = mysql_query($qu ery) or die(mysql_error ());
      $row= mysql_fetch_arr ay($result);
      echo $row['Message'];
      --
      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

      • Kimmo Laine

        #4
        Re: fetch_array()

        "Geoff Berrow" <blthecat@ckdog .co.ukwrote in message
        news:0g9mo2597o f364e5tqafja4e4 8gr1uvg5s@4ax.c om...
        Message-ID: <1166741903.577 897.67580@80g20 00cwy.googlegro ups.comfrom
        Jerim79 contained the following:
        >
        >>$random=rand( 1,5);
        >>$query="SELEC T Message FROM random_quote WHERE MessageID=$rand om";
        >>$message = mysql_query($qu ery) or die(mysql_error ());
        >>echo $message;
        >>
        >>But it only returns "Resource #5." I had someone mention
        >>mysql_fetch_a rray, but that seems like an unecessary way to go about
        >>it. To me, logically the code above should work.
        >
        No, it returns a resource, as you have found out. The output you
        require may just be one thing, but a query can return many rows and
        columns of data. It needs further processing before you can do anything
        useful with it. You might only be interested in the number of rows
        where you could use mysql_num_rows( )
        >
        But in your case, since you know that MessageID is unique you know it
        will only return one row.
        >
        $random=rand(1, 5);
        $query="SELECT Message FROM random_quote WHERE MessageID=$rand om";
        $result = mysql_query($qu ery) or die(mysql_error ());
        $row= mysql_fetch_arr ay($result);
        echo $row['Message'];

        Better use mysql_fetch_ass oc or pass the parameter MYSQL_ASSOC than use
        mysql_fetch_arr ay as is, there's no need for the indexed elements.

        --
        "Ohjelmoija on organismi joka muuttaa kofeiinia koodiksi" - lpk
        http://outolempi.net/ahdistus/ - Satunnaisesti päivittyvä nettisarjis
        spam@outolempi. net | rot13(xvzzb@bhg byrzcv.arg)


        Comment

        • =?iso-8859-1?Q?Kim_Andr=E9_Aker=F8?=

          #5
          Re: fetch_array()

          Kimmo Laine wrote:
          "Geoff Berrow" <blthecat@ckdog .co.ukwrote in message
          news:0g9mo2597o f364e5tqafja4e4 8gr1uvg5s@4ax.c om... >Message-ID:
          <1166741903.577 897.67580@80g20 00cwy.googlegro ups.comfrom
          Jerim79 contained the following:
          $random=rand(1, 5);
          $query="SELECT Message FROM random_quote WHERE MessageID=$rand om";
          $message = mysql_query($qu ery) or die(mysql_error ());
          echo $message;
          >
          But it only returns "Resource #5." I had someone mention
          mysql_fetch_arr ay, but that seems like an unecessary way to go
          about it. To me, logically the code above should work.
          No, it returns a resource, as you have found out. The output you
          require may just be one thing, but a query can return many rows and
          columns of data. It needs further processing before you can do
          anything useful with it. You might only be interested in the
          number of rows where you could use mysql_num_rows( )

          But in your case, since you know that MessageID is unique you know
          it will only return one row.

          $random=rand(1, 5);
          $query="SELECT Message FROM random_quote WHERE MessageID=$rand om";
          $result = mysql_query($qu ery) or die(mysql_error ());
          $row= mysql_fetch_arr ay($result);
          echo $row['Message'];
          >
          Better use mysql_fetch_ass oc or pass the parameter MYSQL_ASSOC than
          use mysql_fetch_arr ay as is, there's no need for the indexed elements.
          Why go through all the hoops of using arrays when you only need the one
          element?

          $random=rand(1, 5);
          $query="SELECT Message FROM random_quote WHERE MessageID=$rand om";
          $result = mysql_query($qu ery) or die(mysql_error ());
          echo mysql_result($r esult, 0);

          --
          Kim André Akerø
          - kimandre@NOSPAM betadome.com
          (remove NOSPAM to contact me directly)

          Comment

          • Geoff Berrow

            #6
            Re: fetch_array()

            Message-ID: <4v41ucF1ahjbpU 1@mid.individua l.netfrom Kim André Akerø
            contained the following:
            $random=rand(1, 5);
            $query="SELECT Message FROM random_quote WHERE MessageID=$rand om";
            $result = mysql_query($qu ery) or die(mysql_error ());
            $row= mysql_fetch_arr ay($result);
            echo $row['Message'];
            >>
            >Better use mysql_fetch_ass oc or pass the parameter MYSQL_ASSOC than
            >use mysql_fetch_arr ay as is, there's no need for the indexed elements.
            >
            >Why go through all the hoops of using arrays when you only need the one
            >element?
            For the novice, the less functions you have to learn the easier it
            becomes. My way, you get rather more than you need, but it works in
            every instance.

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

            • hackajar@gmail.com

              #7
              Re: fetch_array()

              This works too:

              $random=rand(1, 5);
              $query="SELECT Message FROM <database>.rand om_quote WHERE
              MessageID=$rand om"; //Don't forget to include your database!
              $message = mysql_result(my sql_query($quer y),0) or die(mysql_error ());

              Hackajar
              Kim André Akerø wrote:
              Kimmo Laine wrote:
              >
              "Geoff Berrow" <blthecat@ckdog .co.ukwrote in message
              news:0g9mo2597o f364e5tqafja4e4 8gr1uvg5s@4ax.c om... >Message-ID:
              <1166741903.577 897.67580@80g20 00cwy.googlegro ups.comfrom
              Jerim79 contained the following:
              >
              $random=rand(1, 5);
              $query="SELECT Message FROM random_quote WHERE MessageID=$rand om";
              $message = mysql_query($qu ery) or die(mysql_error ());
              echo $message;

              But it only returns "Resource #5." I had someone mention
              mysql_fetch_arr ay, but that seems like an unecessary way to go
              about it. To me, logically the code above should work.
              >
              No, it returns a resource, as you have found out. The output you
              require may just be one thing, but a query can return many rows and
              columns of data. It needs further processing before you can do
              anything useful with it. You might only be interested in the
              number of rows where you could use mysql_num_rows( )
              >
              But in your case, since you know that MessageID is unique you know
              it will only return one row.
              >
              $random=rand(1, 5);
              $query="SELECT Message FROM random_quote WHERE MessageID=$rand om";
              $result = mysql_query($qu ery) or die(mysql_error ());
              $row= mysql_fetch_arr ay($result);
              echo $row['Message'];
              Better use mysql_fetch_ass oc or pass the parameter MYSQL_ASSOC than
              use mysql_fetch_arr ay as is, there's no need for the indexed elements.
              >
              Why go through all the hoops of using arrays when you only need the one
              element?
              >
              $random=rand(1, 5);
              $query="SELECT Message FROM random_quote WHERE MessageID=$rand om";
              $result = mysql_query($qu ery) or die(mysql_error ());
              echo mysql_result($r esult, 0);
              >
              --
              Kim André Akerø
              - kimandre@NOSPAM betadome.com
              (remove NOSPAM to contact me directly)

              Comment

              • =?iso-8859-1?Q?Kim_Andr=E9_Aker=F8?=

                #8
                Re: fetch_array()

                hackajar@gmail. com wrote:
                This works too:
                >
                $random=rand(1, 5);
                $query="SELECT Message FROM <database>.rand om_quote WHERE
                MessageID=$rand om"; //Don't forget to include your database!
                $message = mysql_result(my sql_query($quer y),0) or die(mysql_error ());
                Or even better:

                $query = "SELECT Message FROM random_quote ORDER BY RAND() LIMIT 1";
                $message = mysql_result(my sql_query($quer y),0) or die(mysql_error ());

                --
                Kim André Akerø
                - kimandre@NOSPAM betadome.com
                (remove NOSPAM to contact me directly)

                Comment

                Working...