我在OOP Php中有这个代码
include ('connection.php');
class NestedSet
{
/*Properties*/
/**
* Mysqli object
* @var object
*/
protected $db;
/**
* Name of the database table
* @var string
*/
public $table = 'tree';
/**
* Primary key of the database table
* @var string
*/
public $pk = 'id';
/**
* Namefield in the database table
* @var unknown_type
*/
public $name = 'name';
/*Methods*/
/**
* Stores a Mysqli object for further use
* @param object $mysqli Mysqli object
* @return boolean true
*/
public function __construct() {
$this->db = mysqliConn::init();
return true;
}
protected static $instance = NULL;
// public static function get_instance()
// {
// //if ( NULL === self::$instance )
// // self::$instance = new self;
// // return self::$instance;
// }
/**
* Creates the root node
* @param string $name Name of the new node
* @return boolean true
*/
public function createRootNode($name) {
$this->db->query("LOCK TABLES " . $this->table . " WRITE");
$sql = "SELECT rgt FROM " . $this->table . " ORDER BY rgt DESC LIMIT 1";
$result = $this->db->query($sql);
if ($this->db->affected_rows == 0) {
$lft = 1;
$rgt = 2;
} else {
$obj = $result->fetch_object();
$lft = $obj->rgt + 1;
$rgt = $lft + 1;
}
$sql = "INSERT INTO " . $this->table . " (" . $this->name . ", lft, rgt) VALUES ('" . $name . "', " . $lft . ", " . $rgt . ");";
$this->db->query($sql);
$this->db->query("UNLOCK TABLES");
return true;
}
}
?>
我在另一个名为index.php
的文件中为类NestedSet创建一个新对象 <?php
include("nested_set.php");
$nested = new NestedSet(); //Create a NestedSet object
$nested->createRootNode('root');
?>
我可以在db上写,但$ rgt和$ lft保持2和1; 并显示此错误:
"Notice: Undefined property: mysqliConn::$affected_rows in C:\wamp\www\hr-test\nested_set.php on line 67"
关于我做错什么的任何想法? 谢谢!!
CODE FOR connection.php
<?php
define('SERVER', 'localhost');
define('USERNAME', 'root');
define('PASSWORD', '');
define('DATABASE', 'hr_test2');
class mysqliConn
{
private static $instance;
private $connection;
private function __construct()
{
$this->connection = new mysqli(SERVER,USERNAME,PASSWORD,DATABASE);
}
public static function init()
{
if(is_null(self::$instance))
{
self::$instance = new mysqliConn();
}
return self::$instance;
}
public function __call($name, $args)
{
if(method_exists($this->connection, $name))
{
return call_user_func_array(array($this->connection, $name), $args);
} else {
trigger_error('Unknown Method ' . $name . '()', E_USER_WARNING);
return false;
}
}
} ?&GT;
答案 0 :(得分:2)
由于mysqli->query()
会返回一个mysqli_result
对象,其中包含有关查询结果的信息,您需要使用$result
而不是$this->db->
mysqli_result
对象也不包含affected_rows
属性,您应该使用确实存在的num_rows
属性,而是使用$result
对象。
您也可以简化您创建的查询字符串的连接,尽管您应该使用预准备语句。
public function createRootNode($name) {
$this->db->query("LOCK TABLES " . $this->table . " WRITE");
$sql = "SELECT rgt FROM " . $this->table . " ORDER BY rgt DESC LIMIT 1";
$result = $this->db->query($sql);
// if ($this->db->affected_rows == 0) {
if ($result->num_rows == 0) {
$lft = 1;
$rgt = 2;
} else {
$obj = $result->fetch_object();
$lft = $obj->rgt + 1;
$rgt = $lft + 1;
}
$sql = "INSERT INTO {$this->table} ( {$this->name}, lft, rgt)
VALUES ('$name', $lft , $rgt)";
$this->db->query($sql);
$this->db->query("UNLOCK TABLES");
return true;
}