简体   繁体   中英

php pdo mysql connect - confusing syntax problem

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

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();
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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM