0

I am using PHP with OOP to select rows from the database (MySQL).

When I execute the query, it returns an empty row.

Here is the classe I am using:

<?php 
class EmploiManager
{
 private $_db; 

 public function __construct($db)
 {
   $this->setDb($db);
 }
 public function category($category)
 {
   $q = $this->_db->prepare('SELECT * FROM DemandeEmploi WHERE category = :category');
   $q->execute(array('category' =>$category));
   $donnees = $q->fetch(PDO::FETCH_ASSOC);
   return new Emploi($donnees);
 }

 public function setDb(PDO $db)
 {
  $this->_db = $db;
 }
} 

$type = $_GET['category'];
$manager = new EmploiManager($db);
$row = $manager->category($type);

foreach ($row as $demandeE) 
{
  ?> 
    <div class="list"><h4><a href="?id=<? echo $demandeE->id();?>&category=demandemploi"><? echo $demandeE->title();?></a></h4> </div>      
    <?php
       } 
   ?>

Can any one tell me what's wrong with that code? Thanks!

4
  • Why not do error handling with try/catch block? Commented Jul 27, 2013 at 0:52
  • I tried It always shows nothing, but when I use query() instead of prepare() it gives this error: Warning: PDO::query(): SQLSTATE[42S22]: Column not found: 1054 Unknown column 'demploi' in 'where clause' in /opt/lampp/htdocs/gratisannonces.com/system/class/EmploiManager.class.php on line 28 . But the demploi it exists in the database in the column: category Commented Jul 27, 2013 at 0:58
  • You are saying demoloi your script says demploi and your code shows DemandeEmploi those are three different tables. Check it, because that is where your problem relies Commented Jul 27, 2013 at 1:02
  • yes yes I know :D , if you notice I changed demoloi into demploi it was an error of typing. demploi it is not the name of the table, it's a value in the database. Commented Jul 27, 2013 at 1:09

2 Answers 2

1

It's my bad, I didn't use a loop to select all the rows.

I corrected the code and it works fine now, here is what it looks like:

public function category($category)
{
  $datas = array();

  $q = $this->_db->prepare('SELECT * FROM DemandeEmploi WHERE category = :category');
  $q->execute(array('category' =>$category));

  while ($donnees = $q->fetch(PDO::FETCH_ASSOC)) 
  {
    $datas[] = new Emploi($donnees);
  }
  return $datas;

}
Sign up to request clarification or add additional context in comments.

2 Comments

If your solution works for you, than you should accept the answer.
Oh sorry, I'm new here ^^ It says I can accept it tomorrow, so I will do then. Thank you!
0

$q->fetch() just returns one row of the results. If you want all the results, you must use $q->fetchAll().

Since you specified PDO::FETCH_ASSOC, the elements of $row will be associative arrays, not objects; aren't you getting errors saying that you're trying to call a method on a non-object? So $demandeE->id() should be $demandeE['id'], and $demandeE->title() should be $demandeE['title'].

Alternatively, you could specify PDO::FETCH_OBJ. Then, the values will be properties, not methods, so it should be $demandeE->id and $demandeE->title (no parentheses).

1 Comment

Thanks for your reply. No I'm not getting any error saying that i'm trying to call a method on a non-object, because I'm not getting the id and the title directly from the database, I am getting them form the class Emploi that defines the setters and the getters and the columns that exists in the database: id, category, title... I tired changing fetch() by fetchAll() but still the same problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.