Jump to content

PHP class problem


Olli

Recommended Posts

Hi,

this php code not working for some reason

class db {

public function connect() {
$yhteys = new PDO("mysql:host=*****;dbname=*****", "*******", "******");
}

public function get($query, $parameters){
$prepare	= $yhteys->prepare($query);
$prepare->execute($parameters);
$result		= $prepare->fetch();
$count		= $prepare->rowCount();
return array($result, $count);
}

}

 

$db = new db();
$content = $db->get("SELECT * FROM tuotteet WHERE id = ?", array("1"));
print $content; 

 

It gives error:Parse error: syntax error, unexpected T_OBJECT_OPERATOR

 

Thank you for help!

Link to comment
Share on other sites

class db {
var $yhteys; // could be private or public etc..
public function connect() {
	$this->yhteys = new PDO("mysql:host=*****;dbname=*****", "*******", "******");
}

public function get($query, $parameters){
	$prepare	= $this->yhteys->prepare($query);
	$prepare->execute($parameters);
	$result		= $prepare->fetch();
	$count		= $prepare->rowCount();
	return array($result, $count);
}

}

Link to comment
Share on other sites

Do you know if the PDO is successful?

public function connect() {
try {
	$this->yhteys = new PDO("mysql:host=*****;dbname=*****", "*******", "******");
} catch (PDOException $e) {
	echo 'Could not connect: ' . $e->getMessage();
}
}

Link to comment
Share on other sites

Is this good way to do it? Or should I use just fetchAll ?

 

class db {
private $yhteys;

function __construct() {
	try {
		$this->yhteys = new PDO("................");
	} catch (PDOException $e) {
	echo 'Could not connect: ' . $e->getMessage();
	}
}

private function send_query($query, $parameters, $method){
$prepare	= $this->yhteys->prepare($query);
$prepare->execute($parameters);

if($method == "all"){
$result		= $prepare->fetchAll();
} else {
$result		= $prepare->fetch();
}
$count		= $prepare->rowCount();
return array($result, $count);
}	

public function get($query, $parameters){
$this->sendQuery($query, $parameters, "all");
}

public function getAll($query, $parameters){
$this-sendQuery($query, $parameters, "fetch");
}

}

Link to comment
Share on other sites

That code will not work, (not to mention you have getAll() calling fetch and get() calling all) if you call get() it will run send_query() and return an array to get(), but get() does nothing with the array.

You could re-write your get() to do it all.

public function get($query, $parameters, $method = 'all'){
$prepare	= $this->yhteys->prepare($query);
$prepare->execute($parameters);

if($method == "all"){
	$result		= $prepare->fetchAll();
} else {
	$result		= $prepare->fetch();
}
$count		= $prepare->rowCount();
return array($result, $count);
}

With that code if you call get without the $method paramter it will automatically fetch all.

Link to comment
Share on other sites

yes thats true.

But now I have new question:

Every time I call some class it does again the same construct function:

class product {

public function getId($from, $id){

	switch($from){
		case "seo":
		$query	= "SELECT id FROM tuotteet WHERE seo = ?";
		break;

		case "normal":
		$query	= "SELECT id FROM tuotteet WHERE nimi = ?";
		break;
	}

$result	= $db->get($query, array($id));

return $result[0];

}

public function details($id){

$result	= $db->get($query, array($id));

return $result;

}

}

 

Example, if I need to use both of those functions in one page it creates $yhteys two times. Is that bad?

Link to comment
Share on other sites

You should pass the created $db variable into any other class that needs it to function.

Create a __construct() function for the product class aswell as a $db variable for in the class.

function __construct($db) {
$this->db = $db;
}

so now

$p = new product();

should be called using

$p = new product($db);

As long as the db class is called BEFORE product.

Once you have done this.. the query functions in the product class will have to be called like

$result	= $this->db->get($query, array($id));

 

Make sense? (There is a lot of db flying around in this post)

Link to comment
Share on other sites

Ok, i will try to understand.

But how can I then call product->details ?

 

Example print product->details("1");

 

How would I pass the $yht = new db(); there ?

 

and yes I know I could do it like:

$db = db();
$something = product($db);
print $something->details("4");

But isn´t there any easier way?

Link to comment
Share on other sites

That is the easy way.. Once you have passed in the DB into the product class (as I suggested) it will be accessable by all the function inside the product class.

Having said that. You product class details() function does not have $query defined inside it.

It might be a good idea to read this.

Link to comment
Share on other sites

Thank you for helping. Now I added query and it still doesnt work:

 

class db {
private $connection;

function __construct() {
	try {
		$this->connection	= new PDO(".................");
	} catch (PDOException $e) {
		echo 'Yhteysvirhe: ' . $e->getMessage();
	}
}

public function get($query, $parameters, $method = 'all'){
$prepare	= $this->connection->prepare($query);
$prepare->execute($parameters);

if($method == "all"){
	$result		= $prepare->fetchAll();
} else {
	$result		= $prepare->fetch();
}
$count		= $prepare->rowCount();
return array($result, $count);
}

}

class product {

function __construct($db) {
$this->db = $db;
}

public function getId($from, $id){

	switch($from){
		case "seo":
		$query	= "SELECT * FROM tuotteet WHERE id = ?";
		break;

		case "normal":
		$query	= "SELECT * FROM tuotteet WHERE nimi = ?";
		break;
	}

$result	= $this->$db->get($query, array($id));
return $result[0];

}

public function details($id){

$query 	= "SELECT * FROM tuotteet WHERE id = ?";	
$result	= $this->$db->get($query, array($id));
return $result;

}

}

 

and test file

 

$yht = new db();
print product->details("1");

 

(no error message, just gives empty page)

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.