I am working on something. I am trying to Create a class in config.php that contains a method to connect to the database.In dbConnectExample.php, instantiate that class and call the method. I am kinda getting confused because I am learning to others programming classes too. This is what I have so far;
<?php # Scripts 6.3 - Rectangle.php
Class Rectangle {
//Declare the attributes:
public $width = 0;
public $height = 0;
//Method to set the dimensions.
Function set_size($w = 0, $h = 0) {
$this->width = $w;
$this->height = $h;
}
//Method to calculate and return the area.
function get_area() {
return ($this->width * $this->height);
}
// Method to calculate and return the perimeter.
function get_perimeter() {
return ( ( $this->width + $this->height) * 2 );
}
//Method to determine if the rectangle
//is also a square
function is_square() {
if ($this->width == $this->height) {
return true; //Square
} else {
return false; //Not a square
}
}
} //End of Rectangle class.
I know I am creating a class with a method. I am just confused to connect to a database you go like this;
//Connect to the database
$DBConnect = @new mysqli("localhost", "valerie_2shuawna", "norris");
if (mysqli_connect_errno())
die("<p>Unable to connect to the database server.</p>"
. "<p>Error code " . mysqli_connect_errno()
. ": " . mysqli_connect_error()) . "</p>";
$DBName = "config.php";
@$DBConnect->select_db($DBName)
Or die("<p>Unable to select the database.</p>"
. "<p>Error code " . mysqli_errno($DBConnect)
. ": " . mysqli_error($DBConnect)) . "</p>";
$DBConnect->close();
But I am suppose to instantiate that class and call the method. and then the config it is suppose to contain a method to connect to the database. If someone can help me figure out what I am doing wrong and maybe explain so I am not so confused I would be grateful it.
You can create a simple class: