SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Thread: PDO get value

Hybrid View

  1. #1
    SitePoint Addict
    Join Date
    Dec 2011
    Posts
    221
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)

    PDO get value

    Is there any way to write an efficient code better than this.
    Code:
    try{
                    $sql = "SELECT * FROM device.monitor WHERE del_stat IS NULL AND id = ?";
                    $q = connection::$db->prepare($sql);
                    $q -> bindValue(1, $id, PDO::PARAM_INT);
                    $q ->execute();
                   
                    $row = $q->rowCount();
                            if ($row == 0) {
                                echo 'no records found.';
                            }else{
                                
                               while ($row = $q->fetch(PDO::FETCH_ASSOC))
                                {
                                  $mon = $row['mon_brand'];
                                  $ay= $row['id'];
                                  $return[] = array($mon, $ay);                             
                                                              
                                }
                                                       }
            }catch (Exception $e) {
                print "Error!:" . $e->getMessage();
            }
    because everytime I call the values I just do the following:
    Code:
    foreach ($view as $test)
    {
      $id = $test[1]; 
      $brand = $test[0];
    }
    I wonder if there is another way of doing it like calling just automatically call the id and it will show its value.
    Thank you in advance.

  2. #2
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,496
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    I'm not completely sure to understand what you're asking?
    You pass the id in the query, so it should return only one row? What do you need the while loop for?
    And I don't see any connection between the two pieces of code you posted?

  3. #3
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,122
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    If you have an associative array (like your first example), you can use extract


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
  •