运行时dbconfig.php
(下面描述)将错误视为:
PHP Fatal error: Call to a member function prepare() on a non-object in Class.crud.php on line 13
PHP Stack trace:
PHP 1. {main}() dbconfig.php:0
PHP 2. crud->create() dbconfig.php:16
文件: dbconfig.php
<?php
$db_host = "localhost";
$db_user = "root";
$db_pass = "95111991";
$db_name = "php_pdo_db";
try{
$db_con = new PDO("mysql:host={$db_host}; dbname = {$db_name}",$db_user,$db_pass);
$db_con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch (PDOException $e){
echo $e->getMessage();
}
include_once 'Class.crud.php';
$crud = new crud($db_con);
?>
文件: Class.crud.php
<?php
class crud
{
private $db;
function _construct($db_con)
{
$this->db = $db_con;
}
public function create($fname,$lname,$email)
{
try{
$stmt = $this->db->prepare("INSERT INTO tbl_user(Firstname,Lastname,Email) VALUES (:fname,:lname, :email)");
$stmt ->bindparam(":fname", $fname);
$stmt ->bindparam(":lname", $lname);
$stmt ->bindparam(":email", $email);
$stmt->execute();
return true;
}catch (PDOException $e){
echo $e->getMessage();
return false;
}
}
public function getID($id){
$stmt = $this->db->prepare("SELECT * FROM tbl_user WHERE ID = :id");
$stmt->execute(array(":id"=>$id));
$editRow=$stmt->fetch(PDO::FETCH_ASSOC);
return $editRow;
}
public function update($id,$fname,$lname,$email)
{
try{
$stmt=$this->db->prepare("UPDATE tbl_user SET Firstname = :fname,
Lastname = :lname, Email = :email WHERE ID = :id");
$stmt->bindparam(":fname",$fname);
$stmt->bindparam(":lname",$lname);
$stmt->bindparam(":email",$email);
$stmt->bindparam(":id",$id);
$stmt->execute();
return true;
}catch (PDOException $e){
echo $e -> getMessage();
return false;
}
}
public function delete($id)
{
$stmt = $this->db->prepare("DELETE FROM tbl_user WHERE ID = :id");
$stmt->bindparam(":id",$id);
$stmt->execute();
return true;
}
/* paging */
public function dataview($query)
{
//global $db_con
$stmt = $this->db->prepare($query);
$stmt->execute();
if($stmt->rowCount()>0){
while($row=$stmt->fetch(PDO::FETCH_ASSOC))
{
?>
<tr>
<td><?php print($row['ID']); ?></td>
<td><?php print($row['Firstname']); ?></td>
<td><?php print($row['Lastname']); ?></td>
<td><?php print($row['Email']); ?></td>
<td align = "center">
<a href = "edit_data.php?edit_id=<?php print($row['ID']); ?>"><i class = "glyphicon glyphicon-edit"></i></a>
</td>
<td align = "center">
<a href = "delete_data.php?delete_id=<?php print($row['ID']); ?>"><i class = "glyphicon glyphicon-remove-circle"></i></a>
</td>
</tr>
<?php
}
}
else{
?>
<tr>
<td>Nothing here...</td>
</tr>
<?php
}
}
public function paging($query,$records_per_page)
{
$starting_position = 0;
if(isset($_GET["page_no"]))
{
$starting_position = ($_GET["page_no"]-1)*$records_per_page;
}
$query2 = $query." limit $starting_position, $records_per_page";
return $query2;
}
public function paginglink($query,$records_per_page)
{
$self = $_SERVER['PHP_SELF'];
$stmt = $this->db->prepare($query);
$stmt->execute();
$total_no_of_records = $stmt->rowCount();
if($total_no_of_records > 0){
?><ul class = "pagination"><?php
$total_no_of_page = ceil($total_no_of_records/$records_per_page);
$current_page = 1;
if(isset($_GET["page_no"]))
{
$current_page = $_GET["page_no"];
}
if($current_page != 1)
{
$previous = $current_page-1;
echo "<li><a href = '".$self."?page_no=1'>First</a></li>";
echo "<li><a href = '".$self."?page_no=".$previous."'>Previous</a></li>";
}
for($i = 1; $i <= $total_no_of_page; $i++)
{
if($i == $current_page)
{
echo "<li><a href = '".$self."?page_no=".$li."' style = 'color:red;'>".$i."</a></li>";
}
else{
echo "<li><a href = '".$self."?page_no=".$i."'>".$i."</a></li>";
}
}
if($current_page != $total_no_of_page)
{
$next = $current_page+1;
echo "<li><a href = '".$self."?page_no=".$next."'>Next</a></li>";
echo "<li><a href = '".$self."?page_no=".$total_no_of_page."'>Last</a></li>";
}
?></ul><?php
}
}
/* paging */
}
?>
如何解决此错误?
答案 0 :(得分:1)
在你写的class crud
中:
function _construct($db_con)
使用 __construct
代替_construct
function __construct($db_con)