Jump to content

OOP and PDO Question


Devistation

Recommended Posts

Hey everybody, I would first like to say that I am looking forward to becoming part of the community.

 

I've got a bit of a dilema which I have hoping you guys and girls can help me fix, also, I apologise if I have placed this topic in the wrong place.

 

I am quite new to both PDO and OOP so I apologise in advance for any obvious errors.

 

classes.php

 

class dbConnect {

private $dbHost;
private $dbUsername;
private $dbPassword;
private $dbName;

function dbLink() {
	//Database host.
	$dbHost = "localhost";
	//Database username.
	$dbUsername = "username";
	//Database password.
	$dbPassword = "password";
	//Database name.
	$dbName = "name";

	//Open up a connection to the database
	$dbConnection = new PDO("mysql:host=$dbHost;dbname=$dbName", $dbUsername, $dbPassword, array(PDO::ATTR_PERSISTENT => TRUE));

	return $dbConnection;
}

function prepareQuery($statement, $array) {
	$dbConnectTrue = $this->dbLink();

	$dbqueryPrepare = $dbConnectTrue->prepare($statement);
	$dbqueryPrepare->execute(array($array));   //<------ LINE 29
	$queryFetch = $dbqueryPrepare->fetch(PDO::FETCH_ASSOC);

	return $queryFetch;	
}

}

 

phppage.php

 

//Open up the connection to the database class.
$dbConnectionLink = new dbConnect();

$errorReportFetch = $dbConnectionLink->prepareQuery("SELECT * FROM information WHERE id = ? AND name = ?", array("1", "error"));
$errorReportStatus = $errorReportFetch['status'];

if ($errorReportStatus == 1) {
//If error reporting is equal to 1 then this will print any and all PHP errors.
error_reporting(-1);
}

 

My aim is to try and make it so that I send the query to the class function as well as the WHERE clauses in an array, but I keep getting the following errors...

 

Notice: Array to string conversion in classes.php on line 29

 

Warning: PDOStatement::execute(): SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens in classes.php on line 29

 

What I am realistically trying to do with this class is connect to the database and run a query, whether that be a SELECT, INSERT, UPDATE or DELETE query.

 

I hope this makes sense to someone, I am not the best at describing things, and I apologise if there are any trivial mistakes, I have changed a few things around so that it isn't exactly the same as my code (variables ect).

 

Thank you in advance.

Devistation

Link to comment
https://forums.phpfreaks.com/topic/267766-oop-and-pdo-question/
Share on other sites

execute does not take any arguments. You have to call bind_param first.

 

Problem is bind_param() needs values by reference so you can't just call_user_func_array() it. Needs a little more:

function prepareQuery($statement, array $array = array()) {
$dbConnectTrue = $this->dbLink();

$dbqueryPrepare = $dbConnectTrue->prepare($statement);
if ($array) {
	$copy = array();
	foreach ($array as $key => $value) {
		$copy[] =& $array[$key];
	}
	call_user_func_array(array($dbqueryPrepare, "bind_param"), $copy);
}
$dbqueryPrepare->execute();
$queryFetch = $dbqueryPrepare->fetch(PDO::FETCH_ASSOC);

return $queryFetch;
}

Hey requinix,

 

Thanks for getting back to me... I added in the code that you suggested, only now I get the following errors...

 

Warning: call_user_func_array() expects parameter 1 to be a valid callback, class 'PDOStatement' does not have a method 'bind_param' in classes.php on line 37

 

Warning: PDOStatement::execute(): SQLSTATE[HY093]: Invalid parameter number: no parameters were bound in classes.php on line 41

 

Line 37 being the call_user_func_array line and Line 41 being the execute() line.

 

Devistation

I decided to go for a slightly different approach which seems to work.

 

function prepareQuery($statement, $array) {
$dbConnectTrue = $this->dbLink();

$dbqueryPrepare = $dbConnectTrue->prepare($statement);

$arrayStart = 1;

foreach ($array as $arrayStatement) {
	$dbqueryPrepare->bindParam($arrayStart, $arrayStatement);
	$arrayStart++;
}

$dbqueryPrepare->execute();
$queryFetch = $dbqueryPrepare->fetch(PDO::FETCH_ASSOC);

return $queryFetch;
}

 

Seems to be working for me.

 

Thanks for your help though.

 

Devistation

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.