Jump to content

Annoying problem passing variable as parameter


farban6

Recommended Posts

I am experimenting around with PDO, I have a DB handle class and instancing it in a function. The problem is when I want to bind elements in a query, for example I bind a parameter but I can only do it using a varible outside of the object.

 

<?php

$database = new Database("localhost", "fry", "root", ""); 

$database->set_table("usertest");
$database->set_query("SELECT * FROM usertest WHERE user_name = :user_name");
$value = "matt";
$database->prepare_query();
$database->bind("parameter", ":user_name", $value ,PDO::PARAM_STR, 5);
$database->execute();

while($result = $database->fetch())
        {
        echo $result['user_name'].'<br />';
        }
?>

 

 

Is this bad OOP practice? I wanted to put $value = "matt"; into the Database class somehow

 

here is the Database class

 

<?php

class Database{

public $hostname;
public $database;
public $username;
public $password;
public $connection;
public $prepare;
public $query;
public $table;
public $fetch;
public $bind_var;
public $bind_val;


	 function  __construct($hostname, $database, $username, $password)	{

		 $this->hostname = $hostname;
             $this->database = $database;
             $this->username = $username;
             $this->password = $password;

		 $this->Database_connection();


	}

	  public function  Database_connection()	{

		try {

		$this->connection = new PDO('mysql:host='.$this->hostname.';dbname='.$this->database, $this->username, $this->password);


		}
		catch (PDOException $e) {

   					 echo 'Connection failed: ' . $e->getMessage();
			}

	}
	  
	   public function set_query($query)	{
	   	

		$this->query = $query;

	}
	   
	   public function get_query()	{
	   	

		return $this->query;

	}
	  public function set_table($table)	{
	   	

		$this->table = $table;

	}
	   
	   public function get_table()	{
	   	

		return $this->table;

	}
	   
	 public function set_bind_var($var)	{
	   	
		 $bind_var = $var;

	}
		public function set_bind_val($val)	{
	   	
		 $bind_val = $val;

	}

		public function get_bind_var()	{
	   	
		return $this->bind_var;

	}
		public function get_bind_val()	{
	   	
		return $this->bind_val;

	}

	    public function bind($bindtype, $val, $var, $pdo, $num)	{
	    	
	   	

		if ($bindtype == "parameter")	{

		 $result = $this->prepare->bindParam($val,$var,$pdo);


		}


		return $result;
	}


	   
	   public function prepare_query()	{
	   	

		$this->prepare = $this->connection->prepare($this->get_query());



	}
	   
	   public function execute()	{
	   	

		$this->prepare->execute();


	}
	   
	  
		public function fetch()	{
	   	

		 return $this->prepare->fetch();



}




}

?>

 

Yet agian sorry for the sloppy code, this is mainly an experiment, would be very appriciated if someone could help me out with this problem. :)

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.