HELP - mysql_fetch_array

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

    #1

    HELP - mysql_fetch_array

    I am new to PHP and am trying to run a simple query and display the
    result, with no luck. Here is the code I am using.

    <?php

    //start session
    session_start() ;

    //store cmpid from querystring
    $_SESSION['session_cmpid'] = $_GET['cmpid'];
    $campaign=$_SES SION['session_cmpid'];

    //connect to db server
    require dirname(__FILE_ _).'/core/dbconnect_vps.p hp';

    //select db
    $database = "atdata";

    //define sql
    $sql_campaignre direct = "SELECT web_confirmurl
    FROM tbl_marketingca mpaign
    WHERE marketingcampai gnid = '$campaign'";

    //run sql
    $result = mysql_db_query( $database,$sql_ campaignredirec t)
    or die (mysql_error()) ;

    //fetch data
    $campaign_url = mysql_fetch_arr ay($result);

    //display data
    echo 'URL is '.$campaign_url ;

    ?>

    Instead of displaying the data (http://www.domain.com), it returns:

    URL is Array


    I know it's probably a simple oversigt on my part but can't seem to
    figure it out.

    Thanks......... .James
  • Michael Austin

    #2
    Re: HELP - mysql_fetch_arr ay

    james wrote:[color=blue]
    > I am new to PHP and am trying to run a simple query and display the
    > result, with no luck. Here is the code I am using.
    >
    > <?php
    >
    > //start session
    > session_start() ;
    >
    > //store cmpid from querystring
    > $_SESSION['session_cmpid'] = $_GET['cmpid'];
    > $campaign=$_SES SION['session_cmpid'];
    >
    > //connect to db server
    > require dirname(__FILE_ _).'/core/dbconnect_vps.p hp';
    >
    > //select db
    > $database = "atdata";
    >
    > //define sql
    > $sql_campaignre direct = "SELECT web_confirmurl
    > FROM tbl_marketingca mpaign
    > WHERE marketingcampai gnid = '$campaign'";
    >
    > //run sql
    > $result = mysql_db_query( $database,$sql_ campaignredirec t)
    > or die (mysql_error()) ;[/color]

    [color=blue]
    >
    > //fetch data
    > $campaign_url = mysql_fetch_arr ay($result);
    >
    > //display data
    > echo 'URL is '.$campaign_url ;
    >
    > ?>
    >
    > Instead of displaying the data (http://www.domain.com), it returns:
    >
    > URL is Array
    >
    >
    > I know it's probably a simple oversigt on my part but can't seem to
    > figure it out.
    >
    > Thanks......... .James[/color]


    Looking at this might help:

    $link = mysql_connect(' gonzo', $USR);
    if( $link ) {
    mysql_select_db ( $DB, $link );
    if( $sql ) {
    $sql = ereg_replace( ";$", "", $sql );
    $sql = ereg_replace( "\\\\", "", $sql );
    $result = mysql_query( $sql, $link );
    if( $result ) {
    echo( "<p>Results : for '$sql;'.</p>\n" );
    if( $num = mysql_num_rows( $result ) ) {
    echo( "<pre>\n" );
    while( $array = mysql_fetch_row ( $result ) ) {
    while( list($key, $val) = each( $array ) ) {
    echo "$val | ";
    } # end while
    }

    Michael.

    Comment

    • Marian Heddesheimer

      #3
      Re: HELP - mysql_fetch_arr ay

      On 17 Jun 2004 13:01:33 -0700, james@diroddi.c om (james) wrote:
      [color=blue]
      >Instead of displaying the data (http://www.domain.com), it returns:
      >
      >URL is Array[/color]

      that's correct $campaign_url is an array because mysql_fetch_arr ay()
      returns an array ;-)

      try this:
      echo 'URL is '.$campaign_url['web_confirmurl '];

      Regards

      Marian

      --
      Tipps und Tricks zu PHP, Coaching und Projektbetreuun g
      Da ich mich seit einiger Zeit aus der Programmierung von Websites zurückgezogen habe, sind meine Tutorials und Schulungsinhalte nicht mehr länger verfügbar, da sie inzwischen veraltet sind.

      Comment

      • james

        #4
        Re: HELP - mysql_fetch_arr ay

        Marian Heddesheimer <170604.8.nolin k@spamgourmet.c om> wrote in message news:<vkv3d056m 1nvh80icg7e8jb7 mi2nqnl5np@4ax. com>...[color=blue]
        > On 17 Jun 2004 13:01:33 -0700, james@diroddi.c om (james) wrote:
        >[color=green]
        > >Instead of displaying the data (http://www.domain.com), it returns:
        > >
        > >URL is Array[/color]
        >
        > that's correct $campaign_url is an array because mysql_fetch_arr ay()
        > returns an array ;-)
        >
        > try this:
        > echo 'URL is '.$campaign_url['web_confirmurl '];
        >
        > Regards
        >
        > Marian[/color]

        I understand now. Thanks!

        Am I 'fetching' the data correctly by using mysql_fetch_arr ay(), or is
        there better way?

        Comment

        • james

          #5
          Re: HELP - mysql_fetch_arr ay

          Marian Heddesheimer <170604.8.nolin k@spamgourmet.c om> wrote in message news:<vkv3d056m 1nvh80icg7e8jb7 mi2nqnl5np@4ax. com>...[color=blue]
          > On 17 Jun 2004 13:01:33 -0700, james@diroddi.c om (james) wrote:
          >[color=green]
          > >Instead of displaying the data (http://www.domain.com), it returns:
          > >
          > >URL is Array[/color]
          >
          > that's correct $campaign_url is an array because mysql_fetch_arr ay()
          > returns an array ;-)
          >
          > try this:
          > echo 'URL is '.$campaign_url['web_confirmurl '];
          >
          > Regards
          >
          > Marian[/color]

          I understand now. Thanks!

          Am I 'fetching' the data correctly by using mysql_fetch_arr ay(), or is
          there better way?

          Comment

          • Douglas Abernathy

            #6
            Re: HELP - mysql_fetch_arr ay


            "james" <james@diroddi. com> wrote in message
            news:15cff93e.0 406171807.6b490 98f@posting.goo gle.com...[color=blue]
            > Marian Heddesheimer <170604.8.nolin k@spamgourmet.c om> wrote in message[/color]
            news:<vkv3d056m 1nvh80icg7e8jb7 mi2nqnl5np@4ax. com>...[color=blue][color=green]
            > > On 17 Jun 2004 13:01:33 -0700, james@diroddi.c om (james) wrote:
            > >[color=darkred]
            > > >Instead of displaying the data (http://www.domain.com), it returns:
            > > >
            > > >URL is Array[/color]
            > >
            > > that's correct $campaign_url is an array because mysql_fetch_arr ay()
            > > returns an array ;-)
            > >
            > > try this:
            > > echo 'URL is '.$campaign_url['web_confirmurl '];
            > >
            > > Regards
            > >
            > > Marian[/color]
            >
            > I understand now. Thanks!
            >
            > Am I 'fetching' the data correctly by using mysql_fetch_arr ay(), or is
            > there better way?[/color]

            I prefer to use mysql_fetch_ass oc() because you get ONLY the associative
            indices. If you use mysql_fetch_arr ay() you get the associative indices, but
            you also get their numeric equivalents, for example:

            //Suppose the query returned the following database items: user_id,
            username, password
            $query_string = "SELECT * FROM tbl_login";
            $result=mysql_q uery($query_str ing);
            while ($row=mysql_fet ch_array($resul t)) {
            //Do something with $row['user_id'], $row['username'], $row['password']
            }

            But in the above example code, $row[0] also equals $row['user_id'], $row[1]
            equals $row['username'], etc.

            It doesn't affect you for a simple example like this, but sometimes I have
            needed to loop through all of the elements in $row using a foreach
            statement, and the numeric indices messed me up.

            Just a thought...

            Douglas Abernathy



            Comment

            Working...