I coded this up a bit ago, but it's not working.
I realize I could do this simpler, but I'm trying to use OOP.
So here is the code..
database.class.php
<?php
class config {
public $host;
public $user;
public $pass;
function __construct($host=NULL,$user=NULL,$pass=NULL) {
$this->host = $host;
$this->user = $user;
$this->pass = $pass;
}
function __destruct() {}
}
class database {
private $host;
private $user;
private $pass;
private $config;
function __construct($config) {
$this->config = $config;
}
function __destruct() {}
public function openCon() {
mysql_connect($this->host,$this->user,$this->pass);
if (mysql_errno()) {
printf('Failed to connect: %s', mysql_error());
} else {
echo 'Connected to DB successfully.<br/><br/>';
}
}
public function closeCon() {
try {
mysql_close();
echo 'Successfully closed connection.<br/><br/>';
} catch (exception $e) {
echo $e;
}
}
}
?>
index.php
<?php
include('db.class.php');
$con = new config('localhost','root','');
$etc = new database($con);
$etc->openCon();
mysql_select_db('tester') or die(mysql_error());
$query1 = mysql_query('SELECT * FROM person') or die(mysql_error());
$rows = mysql_fetch_array($query1) or die(mysql_error());
echo 'First: ' . $rows['First'];
echo 'Age:' . $rows['Age'];
$etc->closeCon();
?>
I'm sure there are blaring errors in this. Could anyone help me find and fix them?
It says: Access denied for user ''@'localhost' to database 'tester'
Not sure why.
I specified root as the user, but it came back with '' as the user I requested to use.
It got the host and the database right.
There is no password to my local root user, so...
Any ideas?
Okay, the problem is that the host, user and password are never set. Look in the database
constructor. You only set the configuration.
Either set the private database
properties somehow, or use the configuration object in openCon()
.
Or, even better, just scrap the config
object. Right now it plays no significant role. Just change the database
constructor like the following and be done with it:
function __construct($host, $user, $pass) {
$this->host = $host;
$this->user = $user;
$this->pass = $pass;
}
Finally, if the __destruct()
method does not do anything then you do not need to include it.
Your every thing is correct just you need to change in your database class. You pass $config as an object to the constructor of the database class so to access the variable from that object you need little change. It should be
class database {
private $host;
private $user;
private $pass;
private $config;
public $Connectionlink;
function __construct($config) {
$this->host = $config->host;
$this->user = $config->user;
$this->pass = $config->pass;
}
// function __destruct() {} as your destruct function doing noting so you need not to include this.
public function openCon() {
$this->Connectionlink= mysql_connect($this->host,$this->user,$this->pass);
if (mysql_errno()) {
printf('Failed to connect: %s', mysql_error());
} else {
echo 'Connected to DB successfully.<br/><br/>';
}
}
public function closeCon() {
try {
mysql_close();
echo 'Successfully closed connection.<br/><br/>';
} catch (exception $e) {
echo $e;
}
}
}
mysql_select_db need two parameters, so it should be
mysql_select_db('tester', $etc->Connectionlink) or die(mysql_error());
just try to navigate to your mysql directory using cmd in xampp its "C:\\xampp\\mysql\\bin"
and then type "mysql -u root" and check whether you are getting access
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.