我有一个创建的db.php
文件,用于所有数据库配置和数据库相关的查询功能。
创建functions.php
文件并扩展该db类。
当从functions.php
文件中调用任何方法时,会收到该错误:
致命错误:无法声明类db,因为该名称已经存在 在第3行的C:\ xampp \ htdocs \ Final \ db.php中使用
db.php中
<?php
class db{
private $host = 'localhost';
private $user = 'abc';
private $pass = 'abc123';
private $dbname = 'testphase2';
protected static $mycon;
/*
This function is to connect with DB
*/
public function dbconnect()
{
if(!isset(self::$mycon))
{
self::$mycon = new mysqli($this->host,$this->user,$this->pass,$this->dbname);
}
if(self::$mycon ==false)
{
return false;
}
return self::$mycon;
}
/*
It is used to query the database
*/
public function getrows($sql)
{
$con = $this->dbconnect();
if(!$con)
{
echo "Failed";
}
else
{
//echo "Connected";
$result = mysqli_query($con,$sql) or die(mysqli_error());
}
return $result;
}
/*
It is used to query and fetch the dataset into array and return from the database
*/
public function fetchrows($query)
{
$rows = array();
$result = $this->getrows($query);
if($result== false)
{
return false;
}
while($row = $result->fetch_assoc())
{
$rows[]= $row;
}
return $rows;
}
public function db_close(){
mysqli_close($this->$mycon);
echo "Connection closed";
}
}
?>
的functions.php
<?php
require('db.php');
class functions extends db{
public $currendata = '';
public function fetch_current_Record($id)
{
$con = new db();
$show_existingdata = "Select word from dictionary where id=$id";
$currendata = $con->fetchrows($show_existingdata);
return $currendata;
}
public function get_language()
{
try{
$con = new db();
$query = "SELECT language_id from language";
$result = $con->fetchrows($query);
return $result;
}
catch (Exception $e)
{
echo "Exception MESSAGE: ".$e->getMessage();
}
}
}
?>