<?php
include("Configuration.php");
$MySql = new MySql();
class MySql{
public function Connect(){
mysql_connect($ip, $login, $password); //error Undefined variable: ip,login,password
}
function SelectDatabase(){
}
}
?>
的configuration.php:
<?php
$ip = "localhost";
$db = "website";
$login = "";
$password = "";
?>
答案 0 :(得分:1)
使用global
,或将其作为参数传递。
public function Connect(){
global $ip, $login, $password;
public function Connect($ip, $login, $password){
答案 1 :(得分:1)
将其添加到构造函数中。
include('Configuration.php');
$MySql = new MySql($ip, $login, $password);
你的MySQL课程:
class MySql
{
protected $ip, $login, $password;
public function __construct($ip, $login, $password)
{
$this->ip = $ip;
$this->login = $login;
$this->password = $password;
}
public function Connect()
{
mysql_connect($this->ip,
$this->login,
$this->password);
}
function SelectDatabase(){}
}
为您的进一步发展提供一点建议。您可能想要将您的连接分配给对象中的属性,再次关闭它只是一种很好的方式。它可能看起来像这样:
class MySql
{
protected $ip, $login, $password, $conn;
public function __construct($ip, $login, $password)
{
$this->ip = $ip;
$this->login = $login;
$this->password = $password;
}
public function Connect()
{
$this->conn = mysql_connect($this->ip,
$this->login,
$this->password);
}
public function closeConnection()
{
mysql_close($this->conn);
}
}
是的,正如其他几个人所说的,mysql_connect()
等被弃用了,但我知道你已经知道了。