php的问题,使用类报错了。
刚开始学PHP,今天听完关于类的课了,想实践一下,把原来的数据连接改成类,就出问题了,求高手解答~我用的是MSSQL数据库。这是我改过后的类文件,报错如下,Fataler...
刚开始学PHP,今天听完关于类的课了,想实践一下,把原来的数据连接改成类,就出问题了,求高手解答~ 我用的是MSSQL数据库。
这是我改过后的类文件,报错如下,Fatal error: Allowed memory size of 8388608 bytes exhausted (tried to allocate 24576 bytes) in D:\wamp\www\ckly.php on line 43
是PHP。INI内存不够 吗,为什么我原来的没问题啊,还有个问题,PHP什么情况下使用类啊,举点例子最好了~谢谢了
<?php
class db_mssql{
private $host;
private $dbbase;
private $dbpassword;
function __construct($host,$dbbase,$dbpassword){
$this->host = $host;
$this->dbbase = $dbbase;
$this->dbpassword = $dbpassword;
$this->connnect();
}
//进行连接
function connnect(){
//打开连接
$conn= mssql_connect($this->host,$this->dbbase,$this->dbpassword) or die("连接失败");
mssql_select_db('caipu',$conn) or die ("连接数据库失败");
}
}
$p = new db_mysql('SKY-20120512YAT','sa','123456'); //进行实例化
?>
下边是我原来没用类前的文件没问题,
<?php
$conn = mssql_connect("SKY-20120512YAT","sa","123456")or die ("服务器链接失败!");
mssql_select_db("caipu",$conn) or die ("数据库链失败");
?> 展开
这是我改过后的类文件,报错如下,Fatal error: Allowed memory size of 8388608 bytes exhausted (tried to allocate 24576 bytes) in D:\wamp\www\ckly.php on line 43
是PHP。INI内存不够 吗,为什么我原来的没问题啊,还有个问题,PHP什么情况下使用类啊,举点例子最好了~谢谢了
<?php
class db_mssql{
private $host;
private $dbbase;
private $dbpassword;
function __construct($host,$dbbase,$dbpassword){
$this->host = $host;
$this->dbbase = $dbbase;
$this->dbpassword = $dbpassword;
$this->connnect();
}
//进行连接
function connnect(){
//打开连接
$conn= mssql_connect($this->host,$this->dbbase,$this->dbpassword) or die("连接失败");
mssql_select_db('caipu',$conn) or die ("连接数据库失败");
}
}
$p = new db_mysql('SKY-20120512YAT','sa','123456'); //进行实例化
?>
下边是我原来没用类前的文件没问题,
<?php
$conn = mssql_connect("SKY-20120512YAT","sa","123456")or die ("服务器链接失败!");
mssql_select_db("caipu",$conn) or die ("数据库链失败");
?> 展开
2个回答
2012-05-23 · 知道合伙人软件行家
关注
展开全部
Fatal error: Allowed memory size of 8388608 bytes exhausted (tried to allocate 24576 bytes) in D:\wamp\www\ckly.php on line 43
从错误提示来看,是指php设置薯兆的8M内存已经耗尽。
但错误提示的 ckly.php 第 43 行,这个错误的行号比较奇怪,从你给出的源代码来看这个43行是不存在的。
由于本地没有mssql环境,不能测试,以下是我用mysql环境编写的代码,经测试通过运行正常,供参考:
<?php
class db_mysql
{
private $host;
private $dbbase;
private $dbpassword;
private $conn;
function __construct($host, $dbbase, $dbpassword)
{
$this->host = $host;
$this->dbbase = $dbbase;
$this->dbpassword = $dbpassword;
$this->connnect();
}
//进行连接
function connnect()
{
//打开连接
$this->conn = mysql_connect($this->host, $this->dbbase, $this->dbpassword) or die("连接失败");
mysql_select_db('books', $this->conn) or die("连接数据库失败");
}
function showdata()
{
$rows = mysql_query('SELECT * FROM customers',$this->conn);
echo '<table border="1"><tr><td>姓名</td><td>年龄</td></tr>';
while($row = mysql_fetch_assoc($rows)){
echo '<tr><td>'.$row['name'].'</td>';
echo '清轮<数正租td>'.$row['address'].'</td></tr>';
}
}
}
$p = new db_mysql('localhost', 'test', 'test'); //进行实例化
$p->showdata();
?>
从错误提示来看,是指php设置薯兆的8M内存已经耗尽。
但错误提示的 ckly.php 第 43 行,这个错误的行号比较奇怪,从你给出的源代码来看这个43行是不存在的。
由于本地没有mssql环境,不能测试,以下是我用mysql环境编写的代码,经测试通过运行正常,供参考:
<?php
class db_mysql
{
private $host;
private $dbbase;
private $dbpassword;
private $conn;
function __construct($host, $dbbase, $dbpassword)
{
$this->host = $host;
$this->dbbase = $dbbase;
$this->dbpassword = $dbpassword;
$this->connnect();
}
//进行连接
function connnect()
{
//打开连接
$this->conn = mysql_connect($this->host, $this->dbbase, $this->dbpassword) or die("连接失败");
mysql_select_db('books', $this->conn) or die("连接数据库失败");
}
function showdata()
{
$rows = mysql_query('SELECT * FROM customers',$this->conn);
echo '<table border="1"><tr><td>姓名</td><td>年龄</td></tr>';
while($row = mysql_fetch_assoc($rows)){
echo '<tr><td>'.$row['name'].'</td>';
echo '清轮<数正租td>'.$row['address'].'</td></tr>';
}
}
}
$p = new db_mysql('localhost', 'test', 'test'); //进行实例化
$p->showdata();
?>
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询