class mysql {
private $linkid; // PostgreSQL link identifier
private $host; // PostgreSQL server host
private $user; // PostgreSQL user
private $passwd; // PostgreSQL password
private $db; // PostgreSQL database
private $result; // Query result
private $querycount; // Total queries executed
/* Class constructor. Initializes the $host, $user, $passwd
and $db fields. */
function __construct($host, $db, $user, $passwd) {
$this->host = $host;
$this->user = $user;
$this->passwd = $passwd;
$this->db = $db;
}
/* Connects to the PostgreSQL Database */
function connect(){
try{
$this->linkid = @pg_connect("host=$this->host dbname=$this->dbuser=$this->user password=$this->passwd");
if (! $this->linkid)
throw new Exception("Could not connect to PostgreSQL server.");
}catch (Exception $e) {
die($e->getMessage());
}
}
}

解决方案 »

  1.   

    class mysql {
    private $linkid; // PostgreSQL link identifier
    private $host; // PostgreSQL server host
    private $user; // PostgreSQL user
    private $passwd; // PostgreSQL password
    private $db; // PostgreSQL database
    private $result; // Query result
    private $querycount; // Total queries executed
    /* Class constructor. Initializes the $host, $user, $passwd
    and $db fields. */
    function __construct($host, $db, $user, $passwd) {
    $this->host = $host;
    $this->user = $user;
    $this->passwd = $passwd;
    $this->db = $db;
    }
    /* Connects to the PostgreSQL Database */
    function connect(){
    try{
    $this->linkid = @mysql_connect("host=$this->host dbname=$this->dbuser=$this->user password=$this->passwd");
    if (! $this->linkid)
    throw new Exception("Could not connect to PostgreSQL server.");
    }catch (Exception $e) {
    die($e->getMessage());
    }
    }
    }
      

  2.   

    function db_connect()
    {
    $conn = mysql_connect($dbhost, $dbuser, $dbpsw) ;
    mysql_select_db($dbname, $conn) ;
    return $conn;
    }..................
    $db = db_connect();
    mysql_query('select *****', $db);
    ...............
      

  3.   

    PHP自带的MYSQL类
    function db_connect() 
    {
    $conn = new mysqli('localhost', 'username', 'password', 'database');
    return $conn;
    }.....
    $conn = db_connect();
    $result = $conn->query('select */*******');
    ,,,....
      

  4.   

    按照 willko的方法:在db.php中写如下函数:
    function db_connect() 
    {
    $conn = mysql_connect($dbhost, $dbuser, $dbpswd);
    mysql_select_db($dbname, $conn);
    return $conn;
    }然后在2.php页面require db.php这个文件。2.php中的连接部分如下:
    $db = db_connect();
    $result = mysql_query("select ***** ,$db");
    while($row = @mysql_fetch_array ($result)) {
    ........
    }为什么什么都不输出呢?不用这个方法,直接写的话,是没问题的。
      

  5.   

    2.php里面
    require_once('db.php');
    .....
      

  6.   

    随便下载个什么代码里头都就有这个类!
    自己不会写不就用PHPLIB的 db_mysql.inc 文件里头的类!