• PHP Help General Help
  • [RESOLVED] fetchAll() is only returning 1 row from the database no matter what I do. Plz help!

Hi guys.

I am currently working on an application for a customer.

The problem I am having is in regards to 2 extracts of code.

The first extract is the code I am using to fetch all of the rows from a table called 'gifsleft':

The entire class file is below:

gifclass.php:

<?php

require_once("dataobject.php");

class gif extends dataObject{

protected $data = array(
"id" => "",
"gifnameleft" => "",
"gifnameright" => "",
"urltext" => ""
);





public static function getLeftGifs(){

$conn = parent::connect();

$sql = "SELECT * FROM gifsleft ORDER BY id DESC LIMIT 4";

$st = $conn->query($sql);

$leftgifs = array();

foreach( $st->fetchAll() as $gif ){
$leftgifs[] = new gif($gif);
}

return $leftgifs;

}

}

?>

....... and the code of the class it requires is below:

dataobject.php

<?php

require_once("config.php");

abstract class dataObject{

protected $data = array();

public function __construct( $data ){

foreach( $data as $key => $value ){
if( array_key_exists( $key, $this->data ) ){
$this->data[$key] = $value;
}
}

}



public function getValue($field){
if( array_key_exists( $field, $this->data ) ){
return $this->data[$field];
} else{
die("Field not found!");
}
}


public function getValueEncoded( $field ){
return htmlentities( $this->getValue( $field ) );
}



protected static function connect(){
try{
$conn = new PDO( DSN, USERNAME, PASSWORD );
$conn->setAttribute( PDO::ATTR_PERSISTENT, true );
$conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
} catch( PDOException $e ){

die( "connection failed " . $e );

}

return $conn;

}




protected static function disconnect( $conn ){

return $conn = "";

}

}

?>

...... The second extract - the code snippet I am using to try and fetch all of the rows from the database is below:

From my 'index.php' page:

  <div class="col-lg-2 col-md-2 leftcol"> 
  <?php

list( $leftGifs ) = gif::getLeftGifs();

foreach($leftGifs as $leftGif){
echo $leftGif->getValue('urltext');
}
?>
  </div>

This echos absolutely nothing!
It should run through every row and echo all the different url addresses saved on my database.

..... But for some strange reason I am able to retrieve just one value (the newest field value from the latest row) with the code below:

  <div class="col-lg-2 col-md-2 leftcol"> 
  <?php

list( $leftGifs ) = gif::getLeftGifs();


echo $leftGifs->getValue('urltext');

?>
  </div>

It Just doesn't make any sense, as I have used this same techinique in the past and it has never failed me!

Can someone tell me where I am going wrong?

Thanks.
Paul.

    Are you sure that your database tables have any data in them?

      Consider reading the docs on PDOStatement::fetchAll. You could supply additional parameters which might clear things up. You should also be checking the result of fetchall before trying to loop it in case it comes up FALSE. You shoudl always check the result of database connect and query operations before attempting to use them.

        Hi Guys.

        I have figured out where I have been going wrong.

        It all evolves around this snippet:

         list( $leftGifs ) = gif::getLeftGifs(); 

        I changed it to the following:

         $leftGifs = gif::getLeftGifs(); 

        .... and it now works.

        Maybe it wasn't working as it should because I was using the 'list' function on only one value (variable) passed to it?

        Im not sure.

          [man]list[/man] is not a function but a language construct. You use it with a list of variables to take the array on the right-hand side of your var assignment and break it up into those variables. E.g., :

          $info = array('coffee', 'brown', 'caffeine');
          
          // Listing all the variables
          list($drink, $color, $power) = $info;
          echo "$drink is $color and $power makes it special.\n";
          
          // Listing some of them
          list($drink, , $power) = $info;
          echo "$drink has $power.\n";
          
          // Or let's skip to only the third one
          list( , , $power) = $info;
          echo "I need $power!\n";
          

          Your code would have had the effect of taking the first result in the getLeftGifs array and assigning that one element to $leftGifs

          Glad you figured it out.

            Write a Reply...