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
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_quer开发者_Python百科y("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 :
$host
$username
$password
$database
that would be local to your connect()
method.
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 :
$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.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
$host
is not defined anywhere. It needs to be mysql_connect( $this->host, $this->username, $this->password );
per your own code above.
Or perhaps you just copy pasted the mysql connect line?
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.
This is, of course, in addition to properly referring to the objects variables within itself($this->var
).
change to
mysql_connect( $this->host, $this->username, $this->password)or die("cannot connect");
You probably want $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..
精彩评论