4 Replies - 231 Views - Last Post: 14 July 2013 - 03:32 PM Rate Topic: -----

#1 adn258  Icon User is offline

  • D.I.C Addict

Reputation: 11
  • View blog
  • Posts: 761
  • Joined: 31-August 11

Why Isn't This Class Working With Bound Parameters And What Can I

Posted 13 July 2013 - 04:19 PM

class CGetArticles
{
	private $cCategory;
	private $cLimit;
	private $cStatement;
	
	function __construct()
	{
		$dbh = DB();
		$query = 'SELECT * FROM articles WHERE category=? AND activated=1 ORDER BY id DESC LIMIT ?';
		$stmt = $dbh->prepare($query);
		$stmt->bindParam(1, $this->cCategory);
		$stmt->bindParam(2, $this->cLimit, PDO::PARAM_INT);
		$this->cStatement = $stmt;
	}
	
    public function setCategory($category)
    {
   	    $this->cCategory = $category;
    }
	
	public function setLimit($limit)
	{
		if (!is_numeric($limit))
		{
			echo 'This URL Does Not Exist';
			die();
		}
		$this->cLimit = $limit;
	}
	
	public function getArticles()
	{
		$this->cStatement->execute();
		return $this->cStatement;
	}
	
	
}



So I'm trying to neatly create a class for my articles using the code above. It works just fine for the first getArticles call but when I try to set it to something else for a category it doesn't work at all. Why? What should I do?

Here's What I mean


$articleOutput = new CGetArticles();
//output only 5 articles
$articleOutput->setLimit(5);
$articleOutput->setCategory('Psychology');
$articles = $articleOutput->getArticles();

while ($article = $articles->fetch())
{
  ///do stuff here
}

//Then later on the same page I want to change the category like so
$articleOutput->setCategory('Biology');
$articles = $articleOutput->getArticles();

//from this point I can't just bind a different parameter for the category this way but why?  What can be done?



Is This A Good Question/Topic? 0
  • +

Replies To: Why Isn't This Class Working With Bound Parameters And What Can I

#2 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3520
  • View blog
  • Posts: 10,159
  • Joined: 08-June 10

Re: Why Isn't This Class Working With Bound Parameters And What Can I

Posted 13 July 2013 - 04:51 PM

Id suspect a problem with binding the variables. binding requires a reference, which Im not sure a class property provides. fire up PDOs error handling (setting it to something else than no errors) to check if PDO complains about something.

additionally, you can check with ->debugDumpParams() what values you currently work with.
Was This Post Helpful? 1
  • +
  • -

#3 adn258  Icon User is offline

  • D.I.C Addict

Reputation: 11
  • View blog
  • Posts: 761
  • Joined: 31-August 11

Re: Why Isn't This Class Working With Bound Parameters And What Can I

Posted 14 July 2013 - 02:49 PM

Remember this works the first time but it only uses the FIRST set category it continues to use its first set category. Vardump doesn't help it just shows what I just explained. Does anyone know a way around this?
Was This Post Helpful? 0
  • +
  • -

#4 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3520
  • View blog
  • Posts: 10,159
  • Joined: 08-June 10

Re: Why Isn't This Class Working With Bound Parameters And What Can I

Posted 14 July 2013 - 02:56 PM

then your only choice to find out what is actually passed is to modify the query: SELECT category FROM articles WHERE category = ?
Was This Post Helpful? 0
  • +
  • -

#5 adn258  Icon User is offline

  • D.I.C Addict

Reputation: 11
  • View blog
  • Posts: 761
  • Joined: 31-August 11

Re: Why Isn't This Class Working With Bound Parameters And What Can I

Posted 14 July 2013 - 03:32 PM

View PostDormilich, on 14 July 2013 - 02:56 PM, said:

then your only choice to find out what is actually passed is to modify the query: SELECT category FROM articles WHERE category = ?


Ok so this is weird as heck. For some reason the variables DO get assigned but they don't work unless I set both the limit and the category both it's very odd. For example this actually works check this out.


$articleOutput = new CGetArticles();
$articleOutput->setLimit(5);
$articleOutput->setCategory('Biology');
$articles = $articleOutput->getArticles();
while ($row = $articles->fetch())
{
	echo $row['title'];
}
$articleOutput->setLimit(4);
$articleOutput->setCategory('Psychology');
$otherarticles = $articleOutput->getArticles();
while ($row = $otherarticles->fetch())
{
	echo $row['title'];
}


The above code works but before the category is set to Psychology you see another set Limit to 4 as long as that's in there THIS WORKS FINE but NOT if you just do this


$articleOutput->setCategory('Psychology');
$otherarticles = $articleOutput->getArticles();
while ($row = $otherarticles->fetch())
{
	echo $row['title'];
}





Why would this make any difference? How Bizarre right?

This post has been edited by Dormilich: 14 July 2013 - 09:03 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1