3

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_query("SELECT * FROM settings");
$settings = mysql_fetch_array($settings_query);

echo $settings['title']; 

?>

All I am getting is "cannot connect" on my page.

3
  • 2
    I think you should start with PDO it will be more useful for you in the long run. php.net/manual/en/intro.pdo.php Commented Mar 18, 2011 at 6:28
  • I wish I could upvote you a thousand times, Paul. The braindead mysql extension must be purged. Commented Mar 18, 2011 at 6:54
  • I'd second your comment anytime, Charles! Commented May 13, 2013 at 14:58

6 Answers 6

6

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**][1] `$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.

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

1
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).

Comments

0

change to

mysql_connect( $this->host,  $this->username, $this->password)or die("cannot connect");

Comments

0

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");

1 Comment

Any reason why you would put them in strings needlessly?
0
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..

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.