本款php连接mysql数据库连接程序代码是一款比较简单实用的连接代码,希望本教程对各位同学会有所帮助,代码如下:
- class mysql {
- public $sqlserver = 'localhost';
- public $sqluser = 'root';
- public $sqlpassword = '';
- public $database;
- public $last_query = '';
- private $connection;
- private $query_result;
- public function __construct() {}
- public function __destruct() {
- $this->close();
- }
-
-
-
- public function connect($server = null, $user = null, $password = null, $database = null){
- if (isset($server)) $this->sqlserver = $server;
- if (isset($user)) $this->sqluser = $user;
- if (isset($password)) $this->sqlpassword = $password;
- if (isset($database)) $this->database = $database;
- $this->connection = mysql_connect($this->sqlserver, $this->sqluser, $this->sqlpassword);
- if($this->connection){
- if (mysql_select_db($this->database)){
- return $this->connection;
- }else{
- return $this->error();
- }
- }else{
- return $this->error();
- }
- }
-
-
-
- public function query($query, $die = false){
- if ($query != null){
- $this->last_query = $query;
- $this->query_result = mysql_query($query, $this->connection);
- if(!$this->query_result){
- if ($die) die("die: ".$this->query_result);
- return $this->error();
- }else{
- if ($die) die("die: ".$this->query_result);
- return $this->query_result;
- }
- }else{
- echo "empty query cannot be executed!";
- }
- }
-
-
-
- public function getresult(){
- return $this->query_result;
- }
-
-
-
- public function getconnection(){
- return $this->connection;
- }
-
-
-
-
- public function getobject($qry = null){
- if (isset($qry)) $this->query($qry);
- return mysql_fetch_object($this->getresult());
- }
-
-
-
-
- public function getarray($query_id = ""){
- if($query_id == null){
- $return = mysql_fetch_array($this->getresult());
- }else{
- $return = mysql_fetch_array($query_id);
- }
- return $return ? $return : $this->error();
- }
-
-
-
-
- public function getnumrows($qry = null){
- if (isset($qry)) $this->query($qry);
- $amount = mysql_num_rows($this->getresult());
- return emptyempty($amount) ? 0 : $amount;
- }
-
-
-
- public function hasresults($qry = null) {
- if (isset($qry)) $this->query($qry);
- return $this->getnumrows($qry) > 0;
- }
-
-
-
-
- public function getaffectedrows($qry = null, $query_id = null){
- if (isset($qry)) $this->query($qry);
- if(emptyempty($query_id)){
- $return = mysql_affected_rows($this->getresult());
- }else{
- $return = mysql_affected_rows($query_id);
- }
- return $return ? $return : $this->error();
- }
-
-
-
-
- public function getinsertid($connection_link = null){
- return mysql_insert_id(isset($connection_link) ? $connection_link : $this->connection);
- }
-
-
-
-
- public function close(){
- if(isset($this->connection)){
- return @mysql_close($this->connection);
- }
- else {
- return $this->error();
- }
- }
-
-
-
- private function error(){
- if(mysql_error() != ''){
- echo '<b>mysql errorwww.111cn.net</b>: '.mysql_error().'<br/>';
- }
- }
- }
-
-
- $db = new mysql();
- $db->connect("localhost", "root", "123456", "user");
-
-
-
- if ($db->hasresults("select * from userinfo")) {
-
-
- while ($user = $db->getobject()) {
- echo "user $user->username is called $user->password<br /> ";
- }
- }
- else {
- echo "no results where found";
- }
|