Jump to content

Recommended Posts

Posted
function getKappers($limit = 10){	
	$results_array = array();	
	$rs = wire('database')->prepare("SELECT * FROM appointment_kapper WHERE (actief = '1') ORDER BY volgorde LIMIT ?");
	$rs->bindParam(1, $limit, PDO::PARAM_INT);  // ERROR
	$rs->execute([$limit]);	
	while ($row = $rs->fetch()){
		$results_array[] = $row;
	}	
	return $results_array;
}

Gives me this error and I don't understand why.
Error: Class 'ProcessWire\PDO' not found

Anyone?  I suspect PDO::PARAM_INT ?

Posted

The problem is that PDO is outside the PW namespace, which is why the "\" is necessary to get you to the global namespace. That change definitely works for me.

But then you'll get an error with the way you have your execute([$limit]);

I would go with this version instead:

function getKappers($limit = 10) {
    $rs = wire('database')->prepare("SELECT * FROM appointment_kapper WHERE (actief = '1') ORDER BY volgorde LIMIT :limit");
    $rs->bindParam(':limit', $limit, \PDO::PARAM_INT);
    $rs->setFetchMode(\PDO::FETCH_ASSOC);
    $rs->execute();
    return $rs->fetchAll();
}

Note that I also used FETCH_ASSOC so you automatically get an array back without needing to iterate the results to build it up.

  • Like 3
Posted

Oh! Silly me... I forgot that the $limit in the execute was still there... (that's what you get with C/P-ing)

And of course yes, now the backslash is the solution.

@adrian: correct, I should use FETCH_ASSOC (and maybe :limit too)

  • Like 1

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...