SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast
    Join Date
    Feb 2012
    Posts
    49
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

    Question why it not return?

    Dear all i have 1 problem could you please help me
    PHP Code:
    class SelectDatabase{
        function 
    select($connect,$field,$table){
            
    $query "SELECT ".$field." FROM ".$table;
            
    $stmt $connect->prepare($query);
            
    $stmt->execute();
            return 
    $stmt;
        }
        
    //public function __destruct(){}
    }
    $test = new SelectDatabase();
    $test-> select($con,"*","test");
    while(
    $row $test->fetch(PDO::FETCH_ASSOC)){
        echo 
    $row['field1'].' '.$row['field2'].' '.$row['field3'].'<br />';

    when i create function it work perfectly after i add it into the class it error now i have no idea with it
    but the error happened:
    Fatal error: Call to undefined method SelectDatabase::fetch() in C:\wamp\www\localtesting.com\test\test.php on line 84
    please help me

  2. #2
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,136
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    You were close, your code needs to be updated (you are returning the value via the function call, so you need to catch the returned value and use it)
    PHP Code:
    class SelectDatabase
        function 
    select($connect,$field,$table){ 
            
    $query "SELECT ".$field." FROM ".$table
            
    $stmt $connect->prepare($query); 
            
    $stmt->execute(); 
            return 
    $stmt
        } 
        
    //public function __destruct(){} 

    $test = new SelectDatabase(); 
    // you are returning the value via the function call, so you need to catch the returned value and use it
    $statment $test-> select($con,"*","test"); 
    while(
    $row $statment->fetch(PDO::FETCH_ASSOC)){ 
        echo 
    $row['field1'].' '.$row['field2'].' '.$row['field3'].'<br />'


  3. #3
    SitePoint Enthusiast
    Join Date
    Feb 2012
    Posts
    49
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    oh yes thank SitePoint Guru


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
  •