How do I get specific record?

Discussion in 'PHP' started by rebelagent, Jun 5, 2010.

  1. #1
    I created a simple lame CMS but i wanted to be able select a specific record.

    My db looks like

    CMS
    -id
    -detail
    -area

    and so far I've inserted about 3 entries into CMS
    INSERT INTO 'CMS' ('1','blah blah', 'Create');
    INSERT INTO 'CMS' ('2','blah blah', 'success');
    INSERT INTO 'CMS' ('3','blah blah', 'home');

    I know that's not written 100% right but I'm just giving you info to work with.

    Let say I have home.php, create.php, and sucess.php.

    I want to call row 1 (id 1) on page create.php but I specifically just want to call that one record.

    I did a query
    (SELECT detail FROM CMS where id='1');
    $detail = $_POST['detail'];

    And then I did echo $detail later in the script to display id=1's details. I only need that one value to display and I want that specific row.

    I keep looking around and trying out solutions but they all call multiple $rows or values. I just need one value which is identified by id.

    I appreciate the help, if you can lead me in the right direction. I'm getting more into PHP now and some things aren't entirely clear to me yet. I know how to do error checking and such but yeah...

    Oh and register globals is off, so I can't do it the way some have suggested on other sites.
     
    rebelagent, Jun 5, 2010 IP
  2. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #2
    <?php
    $sql = "SELECT detail FROM CMS WHERE id = '1'";
    //execute query..
    $result = mysql_query($sql);
    //turn data from sql query into an array...
    $row = mysql_fetch_array($result);
    //extract specic entry from the array $row...
    $detail = $row['detail'];
    
    //display detail...
    echo $detail;
    ?>
    PHP:
     
    danx10, Jun 5, 2010 IP
  3. cshwebdev

    cshwebdev Active Member

    Messages:
    226
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    80
    #3
    While danx works, I'd do a mysql_result instead of a fetch_array. No need to get an array if your just getting one value.

    
    <?php
    //The query
    $query = "SELECT detail FROM cms WHERE id='1'";
    //Execute your statement
    $result = mysql_query($query);
    //Return the detail filed
    $detail = mysql_result($result, 0, 0);
    
    echo $detail;
    ?>
    
    PHP:
     
    cshwebdev, Jun 5, 2010 IP
  4. rebelagent

    rebelagent Well-Known Member

    Messages:
    876
    Likes Received:
    46
    Best Answers:
    0
    Trophy Points:
    165
    #4
    Okay i solved it thanks guys!
     
    Last edited: Jun 5, 2010
    rebelagent, Jun 5, 2010 IP