This is a very basic thing we can through the constructor to implement the creation of the class is automatically connected to the MySQL server, as long as we set the $name, $pass, $table the value of three variables is good.
The code is as follows |
Copy Code |
Class connectionmysql{ Host Private $host = "localhost"; Username of the database Private $name = "root"; Password of the database Private $pass = ""; Database name Private $table = "phptest"; Encoded form Private $ut = "Utf-8"; constructor function function __construct () { $this->ut= $ut; $this->connect (); } Links to databases function Connect () { $link =mysql_connect ($this->host, $this->name, $this->pass) or Die ($this->error ()); mysql_select_db ($this->table, $link) or Die ("No Database:". $this->table); mysql_query ("SET NAMES ' $this->ut '"); } function query ($sql, $type = ") { if (! ( $query = mysql_query ($sql)) $this->show (' Say: ', $sql); return $query; } Function Show ($message = ', $sql = ') { if (! $sql) echo $message; else echo $message. ' '. $sql; } function Affected_rows () { return Mysql_affected_rows (); } function result ($query, $row) { Return mysql_result ($query, $row); } function Num_rows ($query) { Return @mysql_num_rows ($query); } function Num_fields ($query) { Return Mysql_num_fields ($query); } function Free_result ($query) { Return Mysql_free_result ($query); } function insert_id () { return mysql_insert_id (); } function Fetch_row ($query) { Return mysql_fetch_row ($query); } Function version () { return Mysql_get_server_info (); } function Close () { return Mysql_close (); } Inserting values into the $table table function Fn_insert ($table, $name, $value) { $this->query ("INSERT into $table ($name) value ($value)"); } Deletes a record in table $table based on the $id value function Fn_delete ($table, $id, $value) { $this->query ("Delete from $table where $id = $value"); echo "ID is". $id. "The record was successfully deleted!"; } } Calling methods $db = new Connectionmysql ();
$db->fn_insert (' Test ', ' id,name,sex ', ' ' ', ' hongtenzone ', ' M '); $db->fn_delete (' Test ', ' id ', 1);
?> |
Here I'm going to tell you about constructors
The code is as follows |
Copy Code |
constructor function function __construct () { $this->ut= $ut; $this->connect ();
} |
This page uses the constructor specifically to do not call the database connection class in the function, otherwise the current page will have multiple connections if access to a large server will be out of MySQL has gone of the formulation oh.
http://www.bkjia.com/PHPjc/632934.html www.bkjia.com true http://www.bkjia.com/PHPjc/632934.html techarticle This is a very basic thing we can through the constructor to implement the creation of the class is automatically connected to the MySQL server, as long as we set the $name, $pass, $table the value of three variables is good. to be a substitute for ...