关于php用引用一个文件的方式连接数据库的问题
php文件<?phpClassSql{public$conn;public$dbname;public$username;public$host;}?>ini文件:hos...
php文件
<?php
Class Sql{
public $conn;
public $dbname;
public $username;
public $host;
}
?>
ini文件:
host = localhost
dbname = test
username = root
password = root
用parse_ini_file方法获取信息集合$info 这样$info['host']就能得到localhost 但是怎么整合进去,到class类。新手,要不告诉主流方法也行。 展开
<?php
Class Sql{
public $conn;
public $dbname;
public $username;
public $host;
}
?>
ini文件:
host = localhost
dbname = test
username = root
password = root
用parse_ini_file方法获取信息集合$info 这样$info['host']就能得到localhost 但是怎么整合进去,到class类。新手,要不告诉主流方法也行。 展开
展开全部
<?php
/**
载入配置文件封装SQL类
**/
class Sql{
var $host;
var $username;
var $password;
var $conn;
var $dbname;
function __construct(){
$_ini_ = parse_ini_file("sql.ini");
//测试
/* var_dump($_ini_ );
exit; */
$this->host = $_ini_["host"];
$this->username = $_ini_["username"];
$this->password = $_ini_["password"];
$this->dbname = $_ini_["dbname"];
$this->conn = mysql_connect($this->host ,$this->username, $this->password);
if (! $this->conn) {
die ( "连接数据库失败" . mysql_error () );
}
mysql_query ( "set names utf8" );
//选择数据库
mysql_select_db ( $this->dbName, $this->conn ) or die ( "连接数据库失败" );
}
}
//实例化
new Sql();
?>
希望采纳!
/**
载入配置文件封装SQL类
**/
class Sql{
var $host;
var $username;
var $password;
var $conn;
var $dbname;
function __construct(){
$_ini_ = parse_ini_file("sql.ini");
//测试
/* var_dump($_ini_ );
exit; */
$this->host = $_ini_["host"];
$this->username = $_ini_["username"];
$this->password = $_ini_["password"];
$this->dbname = $_ini_["dbname"];
$this->conn = mysql_connect($this->host ,$this->username, $this->password);
if (! $this->conn) {
die ( "连接数据库失败" . mysql_error () );
}
mysql_query ( "set names utf8" );
//选择数据库
mysql_select_db ( $this->dbName, $this->conn ) or die ( "连接数据库失败" );
}
}
//实例化
new Sql();
?>
希望采纳!
参考资料: 璞玉
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询