Results 1 to 4 of 4

Thread: Retrieving a value from MySQL as a string (efforts at simplification).

  1. #1
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,385
    Thanks
    100
    Thanked 113 Times in 111 Posts

    Default Retrieving a value from MySQL as a string (efforts at simplification).

    Hi, I was just wondering if there was a simpler way to write the following code. This is assuming that I know what row it is located in and what field and that I want just the one value from the table.

    PHP Code:
    $query "SELECT field FROM tablename where ID=1";
      
    $result mysql_query($query)
           or die (
    "Couldn't execute query.");
      
    $value mysql_fetch_array($result,MYSQL_ASSOC);
    $value$value['field'];echo "pop $value pop"
    Thanks for reading

  2. #2
    Join Date
    Jul 2006
    Location
    just north of Boston, MA
    Posts
    1,806
    Thanks
    13
    Thanked 72 Times in 72 Posts

    Default

    not so much as simpler, but you could re-write it to be something like
    Code:
    $q = "SELECT field FROM table WHERE id='#'";
    if( $rs=mysql_fetch_array(mysql_query($q), MYSQL_ASSOC) )
    {
         echo "pop {$rs['field']} pop";
    }
    else {
         echo "Couldn't execute query";
    }

  3. #3
    Join Date
    Sep 2005
    Location
    India
    Posts
    1,627
    Thanks
    6
    Thanked 107 Times in 107 Posts

    Default

    I feel that it is better to have die statement while executing the query.

  4. #4
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,385
    Thanks
    100
    Thanked 113 Times in 111 Posts

    Default

    hmm, thanks for the advice, but it looks like I am using the right code for what I want to do and am not being needlessly complicated. There is some minor rearranging of code that can be done to make this a little more efficient, but I find it easier to read this way.

    I have to agree with codeexploiter here. The die statement has been a valuable tool for me when debugging a script. Mostly I was concerned that I was using around 10 commands when with other built in php functions available I could say the same thing in just 2 or three commands or maybe I was being too server heavy in the layout of it.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •