1
<?php $mysql_host='mysql1.000webhost.com'; $mysql_dbname='a8130617_skola'; $mysql_username='something'; $mysql_password='something'; class mysql { try{ public $db = new PDO("mysql:host=$mysql_host;dbname=$mysql_dbname", $mysql_username, $mysql_password); } catch(PDOException $e){ echo $e->getMessage(); } } //ERROR EXCLAMATION MARK HERE??? ?> 

why does netbeans 6.9.1 consider this to be false syntax? many thanks

2 Answers 2

4

Do you know anything about OOP ?

Class should contain fields and/or methods. You just surrounded a piece of code with class{}. It is not programming.

Read about OOP in PHP - here is manual: http://php.net/manual/en/language.oop5.php

Read it for your own good.

Edit:

I know that following example can make you much lazy but I'll take a shoot and believe you will read more.

Example class for connections can look like:

class Mysql { protected $_host; protected $_dbname; protected $_username; protected $_password; protected $_db; public function __construct($host = null, $dbname = null, $username = null, $password = null) { $this->_host = $host; $this->_dbname = $dbname; $this->_username = $username; $this->_password = $password; } public function connect() { try { $this->_db = new PDO('mysql:host=' . $this->_host . ';dbname=' . $this->_dbname, $this->_username, $this->_password); } catch(PDOException $e){ echo $e->getMessage(); } } public function getDb() { return $this->db; } public function setHost($host) { $this->_host = $host; return $this; } public function getHost() { return $this->_host; } public function setDbname($dbname) { $this->_dbname = $dbname; return $this; } public function getDbname() { return $this->_dbname; } public function setUsername($username) { $this->_username = $username; return $this; } public function getUsername() { return $this->_username; } public function setPassword($password) { $this->_password = $password; return $this; } public function getPassword() { return $this->_password; } } 

And example usage:

$mysql = new Mysql('mysql1.000webhost.com', 'a8130617_skola', 'something', 'something'); $mysql->connect(); 
Sign up to request clarification or add additional context in comments.

3 Comments

that is an excellent demonstration of responsible programming. thank you
My thought on this is: why do you need this? If you have $db = new PDO(..CREDS...); versus $db = new MySQL(); .. its not saving you any time really and adds another unnecessary layer. In this case, specifically. If someone is concerned about having to type out the connection string then just put it in a constant or some other unmuteable var.
It was a presentation of correct using of class and OOP in that case.
0
try{ public $db = new PDO("mysql:host=$mysql_host;dbname=$mysql_dbname", $mysql_username, $mysql_password); } catch(PDOException $e){ echo $e->getMessage(); } 

Try catch blocks need to be inside a method. But going with that, not sure why you are wrapping this in a class? Your class is a wrapper for an already defined class.

3 Comments

Nothing wrong with that, assuming it's done correctly. Be nice to not have to rewrite the weird PDO connection string over and over.
that's way.. ok then. you are right, why would i wrap it inside class when there is a concept called argument?? many thanks
There are things called constants, includes etc. These can all keep it simple. The example below really doesn't save you much. The only thing I would do is make your connection object static, otherwise I don't see the benefit of wrapping it like such. You will end up doubling your coding in the long run.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.