Link to home
Start Free TrialLog in
Avatar of Bruce Gust
Bruce GustFlag for United States of America

asked on

Doing something wrong with this PDO Select Statement...

Here's my code:

class TermAdmin {

	public function term_list($discipline) {
	
	global $mysqli;
	
	$sql=$mysqli->query("select * from terms where discipline=? order by discipline, term");
	$sql->bindValue(1, $discipline, PDO::PARAM_STR);
	$sql->execute();
			
		$result_array = array();
		
		while($row=$sql->fetch(PDO::FETCH_ASSOC))
		{
			$result_array[]=$row;
		}
		return $result_array;
	}
}

$discipline="PHP";
$term_display=$new_list->term_list($discipline);

Open in new window


I get this error:  Call to a member function bindValue() on boolean in C:\wamp64\www\adm\term_class_pdo.php on line 10

How can this be reading a "true" or "false" value? I'm passing "PHP" as discipline, yet I keep getting told I'm going for something else.

What am I missing?
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Use var_dump() to print out the value of $sql after line 7.

If you find that the value is FALSE, reveal the information in $mysqli->error.

Many good examples are available in this article.
https://www.experts-exchange.com/articles/11177/PHP-MySQL-Deprecated-as-of-PHP-5-5-0.html
ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Bruce Gust

ASKER

Hey, Ray!

I figured it out. I was attempting to bind a variable to my SQL without first preparing it, so this:

$sql=$mysqli->query("select * from terms where discipline=? order by discipline, term");
      $sql->bindValue(1, $discipline, PDO::PARAM_STR);
      $sql->execute();

became this...

$sql=$mysqli->prepare("select * from terms where discipline=:discipline order by discipline, term");
      $sql->bindValue(':discipline', $discipline, PDO::PARAM_STR);
      $sql->execute();

...and all was well.

I am using PDO. I will call it something else besides $mysqli. I was just playing in my sandbox, but this is what I'm using:

$mysqli=new PDO('mysql:host=localhost; dbname=bgustgeneral', $user, $password);

The company I'm working for is using a different kind of convention than the global dynamic. Again, I was just knocking out thing at a time, however...

I've got another question at https://www.experts-exchange.com/questions/29003836/How-is-this-connection-happening.html. Would love to get some feedback.