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??

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.