当我在showAll中调用函数connect和closeDb时出现错误。
class DBConnect {
private $con;
private function connect() {
$con = mysql_connect("localhost", "root", "");
mysql_select_db('school');
}
private function closeDb() {
mysql_close($this->con);
}
public function showAll() {
self::connect();
echo "THis is from the showAll method";
self::closeDb();
}
}
答案 0 :(得分:2)
在PHP 5.5.0中不推荐使用Mysql扩展,并在PHP 7.0.0中将其删除。相反,应该使用MySQLi或PDO_MySQL扩展。
学习面向对象编程的基础知识:http://php.net/manual/en/language.oop5.php
答案 1 :(得分:0)
这条线看起来很假。
$con = mysql_connect("localhost", "root", "");
如果你想要做
$this->con = mysql_connect("localhost", "root", "");
然后
public function showAll() {
$this->connect();
echo "THis is from the showAll method";
$this->closeDb();
}
使用$ this!