Home >Backend Development >PHP Tutorial >PHP object-oriented usage tutorial Simple database connection_PHP tutorial
This PHP database connection class should be regarded as the simplest connection class, and it is also the best connection class to understand. As a starting point for starting the PHP programming journey. I have read a lot of MYSQL database connection classes circulated on the Internet. They are all too complicated. In my opinion, there is really no need to make it so complicated, just a small database connection.
The following is the class I wrote:
Class createdb //Start of class
{
var $db="localhost";//Database address;
var $dbname = "root";//User name;
var $ dbpwd = "";//Password;
var $dbtable = "text";//Database used
var $conn; //Database connection;
var $result; //Result set
var $mysql ="select * from text"; //Executed mysql
var $row; //Find data in the result set
function createconn() //This class method starts a conn connection and then starts Select database
{
$this->conn = mysql_connect($this->db,$this->dbname,$this->dbpwd);
mysql_select_db($this-> dbtable,$this->conn);
}
function getresule() //This is to get a result set
{
$this->result = mysql_query($this ->mysql,$this->conn);
}
function getrow() //Create a forward result set pointer
{
$this->row = mysql_fetch_array( $this->result);
return $this->row;
}
}//The class ends and starts calling the class to read mysql database data.
$bb = new createdb; //Class instantiation
$bb->createconn();//Call the connection of the class
$bb->getresule(); //Call the class to get the result set
while($bb->getrow()){ //Call the class to create a pointer and read forward the loop to read the data,
echo($bb->row["voteid"]);
echo($bb->row["vote"]);
}
?>
This is written and not optimized. However, After debugging, you can use it. In the future, I will continue to write more tutorial classes, such as file uploading or other things, and teach you step by step.
There are also css+div layout tutorials and examples, and I will also do it in the future I’ll teach you slowly. In fact, it’s really tiring to make a website
There are so many things to learn.