SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Wizard donboe's Avatar
    Join Date
    Jun 2010
    Location
    Netherlands
    Posts
    2,105
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    fetch single rows

    For an update form I need to fetch singe rows so I created the query like this:
    PHP Code:
    $item_id       $_GET['item_id'];
    $content       $conn->query("SELECT * FROM menu_kaart_items
                            WHERE menu_kaart_item_id = '
    $item_id'"); 
    To fetch the rows I used:
    PHP Code:
    $row $content->fetchAll(PDO::FETCH_ASSOC); 
    But my form fields that look like this:
    HTML Code:
    <input name="menu_item" type="text" class="textfield" value="<?php echo $row->menu_item;?>">
    dont return any results.

    I used MYSQL before where I used:
    PHP Code:
    $content       mysql_query("SELECT * 
                                  FROM menu_kaart_items
                      WHERE menu_kaart_item_id = '
    $item_id'");
    $row mysql_fetch_object($content); 
    which gave me the right results but many people here advised me to switch to PDO which I am working on right now but I can't get my head arround this part.

    What am I doing wrong?
    “Good artists copy, great artists steal” (Pablo Picasso - 1934)

  2. #2
    Community Advisor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,405
    Mentioned
    45 Post(s)
    Tagged
    12 Thread(s)
    Hi donboe, how's it going?

    Calling fetchAll gives you a multidimensional array, even if the query itself only returns a single row. So to access the menu_item column, you'd actually need to do this:
    PHP Code:
    <?php echo $row[0]['menu_item'?>
    As you only want a single row, use the fetch method instead. In your form code you're accessing $row as an object, so you'll also need to specify that you want an object to be returned when you call fetch:
    PHP Code:
    $row $content->fetch(PDO::FETCH_OBJ); 
    "There are two ways of constructing a software design: One way is to make it so simple that there are obviously no deficiencies and the other way is to make it so complicated that there are no obvious deficiencies."

  3. #3
    SitePoint Wizard donboe's Avatar
    Join Date
    Jun 2010
    Location
    Netherlands
    Posts
    2,105
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi fretburner. Yes things are nearly quite well also thanks to you. Something new learned from your reply. Works great. Can I ask another question it'sabout an update query. Or should I start a new threat?
    “Good artists copy, great artists steal” (Pablo Picasso - 1934)

  4. #4
    Community Advisor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,405
    Mentioned
    45 Post(s)
    Tagged
    12 Thread(s)
    Quote Originally Posted by donboe View Post
    Can I ask another question it'sabout an update query. Or should I start a new threat?
    Sure, yeah. If you wanna start a new thread I'd be happy to take a look
    "There are two ways of constructing a software design: One way is to make it so simple that there are obviously no deficiencies and the other way is to make it so complicated that there are no obvious deficiencies."

  5. #5
    SitePoint Wizard donboe's Avatar
    Join Date
    Jun 2010
    Location
    Netherlands
    Posts
    2,105
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I will start a new threat then!
    “Good artists copy, great artists steal” (Pablo Picasso - 1934)


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
  •