Jump to content

How to get prepared statements to work through OOP


farban

Recommended Posts

Hello

 

Got a problem where I want to get prepared statements to work through adding parameters, something like this

 

<?php

class Prepared_Statement extends Handle_DB {

private $query;
private $bind_result;
private $stmt;

public function set_PS_query($query)	{



	$this->query = $query;

}

public function setup_PS($bindresult)	{



	$this->stmt = parent::connect_DB()->stmt_init();

	$this->stmt->prepare($this->query);

	$this->stmt->execute();

	$this->stmt->bind_result($bindresult);


}

	public function show_PS_results($result)	{



	while($this->stmt->fetch())	{

		return $result;
	}


}



public function close_PS()	{


	$this->stmt->close();

}




}


$preS = new Prepared_Statement();

$preS->set_PS_query("SELECT user_name FROM usertest");

$preS->setup_PS("user_name");

echo $preS->show_PS_results("user_name");

$preS->close_PS();


?>

 

The problem is that it throws up Undefined variable: user_name errors when i run the script. I know why this is the case, how can I pass a variable into the prepared statement without it saying its undefined??

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.