SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Apr 2012
    Location
    algeria,constantine
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Unhappy pdo fetch num problem

    hello,
    I have a pdo fetch num problem
    look at my function here:
    PHP Code:
    function tutofilter_tutorials_get($addon ''){
          global 
    $tutofilter_db_handle;
          
    $query sprintf("SELECT * FROM `tutorials` $addon");
          
    $qresult $tutofilter_db_handle->query($query);echo '<br/><pre>'.$query.'</pre><br/>';
          
          if(!
    $qresult)
              return 
    NULL;
          
    $rcount $qresult->fetch(PDO::FETCH_NUM);echo $rcount;
          if(
    $rcount == 0)
              return 
    NULL;
          
    $tutorials = array();
          for(
    $i 0;$i $rcount;$i++)
             
    $tutorials[count($tutorials)] = $qresult->fetch(PDO::FETCH_OBJ);
          
    $qresult NULL;
          return 
    $tutorials;
      } 
    the fetch num return empty array ,this is the problem
    please ,help me.

  2. #2
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    You need to look at the PHP manual again:
    Quote Originally Posted by PHP Manual
    PDO::FETCH_NUM: returns an array indexed by column number as returned in your result set, starting at column 0
    I.e. it doesn't return the number of rows in a result set.

    Heres your code made in a simpler way:
    PHP Code:
    function tutofilter_tutorials_get($addon ''){
          global 
    $tutofilter_db_handle;
          
    $query "SELECT * FROM `tutorials` {$addon}";
          
    $qresult $tutofilter_db_handle->query($query);
          if(
    false === $qresult){
              return 
    null;
          }
          
    $tutorials $qresult->fetchAll(PDO::FETCH_OBJ);
          if(empty(
    $tutorials)){
              return 
    null;
          }
          return 
    $tutorials;

    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  3. #3
    SitePoint Member
    Join Date
    Apr 2012
    Location
    algeria,constantine
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thank you, mr jake

  4. #4
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by promehdioh (via PM)
    hello mr jake,
    I have a question for your reply in my post here http://www.sitepoint.com/forums/show...ch-num-problem
    why you set the $addon variable inside {}?
    thank you
    That's simply my preferred way of inserting variables into strings. The benefits are outlined here.
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona


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
  •