[英]PHP OOP MySQL connect
Ok, so I am a complete beginner at OOP in php, and I thought I would try something easy to start with, however it it not working how I would expect 好吧,所以我是一个完全初学者在PHP的OOP,我想我会尝试一些简单的开始,但它不工作我期望的
<?php
class mySQL{
var $host;
var $username;
var $password;
var $database;
public function connect($set_host, $set_username, $set_password, $set_database){
$this->host = $set_host;
$this->username = $set_username;
$this->password = $set_password;
$this->database = $set_database;
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$database")or die("cannot select DB");
}
}
$connect = new mySQL();
$connect -> connect('localhost', 'user', 'pass', 'database1');
$settings_query = mysql_query("SELECT * FROM settings");
$settings = mysql_fetch_array($settings_query);
echo $settings['title'];
?>
All I am getting is "cannot connect" on my page. 我得到的只是“无法连接”在我的页面上。
In your connect method, you are trying to use the following variables : 在您的connect方法中,您尝试使用以下变量:
$host
$username
$password
$database
that would be local to your connect()
method. 这将是您的
connect()
方法的本地。
But those variables don't exist. 但那些变量不存在。
Instead, if you want to use the properties $host
, $username
, and $password
that are defined in your class, you must use $this
to access them : 相反,如果要使用类中定义的属性
$host
, $username
和$password
,则必须使用$this
来访问它们 :
$this->host
$this->username
$this->password
$this->database
For more informations, take a look at this page of the PHP manual -- and you might want to read more about Classes and Objects . 有关更多信息,请查看PHP手册的此页面 - 您可能想要阅读有关类和对象的更多信息。
mysql_connect("$host", "$username", "$password")or die("cannot connect");
$host
is not defined anywhere. $host
未在任何地方定义。 It needs to be mysql_connect( $this->host, $this->username, $this->password );
它需要是
mysql_connect( $this->host, $this->username, $this->password );
per your own code above. 根据您自己的代码。
Or perhaps you just copy pasted the mysql connect line? 或许你只是复制粘贴mysql连接线?
class mySQL{
var $host;
var $username;
var $password;
var $database;
public $dbh; //Variable for storing connection
public function connect($set_host, $set_username, $set_password, $set_database){
$this->host = $set_host;
$this->username = $set_username;
$this->password = $set_password;
$this->database = $set_database;
$this->dbh = mysql_connect($this->host, $this->username, $this->password)or die("cannot connect"); //Store data connection specifier in object
mysql_select_db($this->database)or die("cannot select DB");
}
public function query($sql)
{
return mysql_query($sql,$this->dbh); //Specify connection handler when doing query
}
public function fetch($sql)
{
return mysql_fetch_array($this->query($sql));
}
}
$connect = new mySQL();
$connect->connect('localhost', 'user', 'pass', 'database1');
$settings_query = mysql_query("SELECT * FROM settings", $connect->dbh); //Specify connection handler when doing query
$settings = mysql_fetch_array($settings_query);
//With query method in object you can do this:
$settings_query = $connect->query("SELECT * FROM settings"); //Use object method to query
$settings = mysql_fetch_array($settings_query);
//with fetch method in object you can just do this:
$settings = $connect->fetch("SELECT * FROM settings");
echo $settings['title'];
Specifying the connection handler, especially easy with a query method, allows you to easily do stuff like: 使用查询方法指定连接处理程序尤其简单,可以轻松地执行以下操作:
$connect1 = new MySQL();
$connect2 = new MySQL();
$connect1->connect('localhost','user','pass','database1');
$connect2->connect('localhost','user','pass','database2');
and easily handle more than one sql object/database connection at once, and helps alleviate any confusion that may arise. 并且可以轻松地同时处理多个sql对象/数据库连接,并有助于缓解可能出现的任何混淆。
This is, of course, in addition to properly referring to the objects variables within itself( $this->var
). 当然,除了正确引用其自身内的对象变量(
$this->var
)之外。
改成
mysql_connect( $this->host, $this->username, $this->password)or die("cannot connect");
You probably want $this
: 你可能想要
$this
:
mysql_connect("{$this->host}", "{$this->username}", "{$this->password}")or die("cannot connect");
mysql_select_db("{$this->database}")or die("cannot select DB");
class mySQL{
public function connect($set_host, $set_username, $set_password, $set_database){
mysql_connect("$set_host", "$set_username", "$set_password")or die("cannot connect");
mysql_select_db("$set_database")or die("cannot select DB");
}
}
Actually this seemed to work fine for me.. 实际上这似乎对我来说很好..
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.