Heim  >  Artikel  >  Backend-Entwicklung  >  php mysql_connect 与mysql_pconnect函数与实例教程_PHP教程

php mysql_connect 与mysql_pconnect函数与实例教程_PHP教程

WBOY
WBOYOriginal
2016-07-13 17:04:27807Durchsuche

当连接的时候本函数将先尝试寻找一个在同一个主机上用同样的用户名和密码已经打开的(持久)连接,如果找到,则返回此连接标识而不打开新连接。 其次,当脚本执行完毕后到 SQL 服务器的连接不会被关闭,此连接将保持打开以备以后使用(mysql_close() 不会关闭由 mysql_pconnect() 建立的连接)。

php教程 mysql教程_connect 与mysql_pconnect函数与实例教程

mysql_connect

 mysql_connect($this->root,$this->user,$this->pass)

/*

mysql_connect ,单个反问用户不会频繁的调用数据库教程,没必要保持连接,而且mysql的连接数也是有限制的, 使用 及时访问比较频繁,也最好使用mysql_connect,这样使用的过的资源可以立刻释放,否则,容易造成资源耗
*/
mysql_pconnect
/*
mysql_pconnect() 函数打开一个到 MySQL 服务器的持久连接
*/

$con = mysql_pconnect("localhost","mysql_user","mysql_pwd");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }


 

class testLinkMysql{ 
  public $conn;
  public $root='localhost';
  public $user='root';//'loupan';
  public $pass='root';//'loupandsffds';
  public $db='dbname';
  public $charset='gbk';
  public $links='c'; //标题
  
  function __construct() {
    if( !$this->conn )
   {
    $this->connect();
   }   
  }
  
    
  function __destruct() {
   if( $this->conn )
   {
          $this->close();
   }
     }
  
  
  function MysqlConnect()
  {
   try{
    if( 'p' == $this->links )
    {
     $this->conn = mysql_pconnect($this->root,$this->user,$this->pass) or die(mysql_error());       
    }
    else
    {
     $this->conn = mysql_connect($this->root,$this->user,$this->pass) or die( mysql_error());
    }
    mysql_select_db($this->db,$this->conn); 
    mysql_query("set Names '$this->charset'");
   }catch (Exception $e){
    echo '数据库连接失败,请联系相关人员!';
    exit;
   } 
  }
  
  function close()
  {
   mysql_close($this->links);
  }
 }


 
 /*
 总结:
 mysql_pconnect() 和 mysql_connect() 非常相似,但有两个主要区别:

当连接的时候本函数将先尝试寻找一个在同一个主机上用同样的用户名和密码已经打开的(持久)连接,如果找到,则返回此连接标识而不打开新连接。
其次,当脚本执行完毕后到 SQL 服务器的连接不会被关闭,此连接将保持打开以备以后使用(mysql_close() 不会关闭由 mysql_pconnect() 建立的连接)。
本站原创教程转载注明来源http://www.bKjia.c0m/phper/php.html

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/630873.htmlTechArticle当连接的时候本函数将先尝试寻找一个在同一个主机上用同样的用户名和密码已经打开的(持久)连接,如果找到,则返回此连接标识而不...
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn