2015-11-04 122 views
1

我有這樣的代碼在面向對象的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的

<?php 


    include("nested_set.php"); 




    $nested = new NestedSet(); //Create a NestedSet object 
    $nested->createRootNode('root'); 

    ?> 

的其他文件中創建的類NestedSet一個新對象我可以在db上寫,但$ rgt和$ lft保持2和1; 這個錯誤displayd:

"Notice: Undefined property: mysqliConn::$affected_rows in C:\wamp\www\hr-test\nested_set.php on line 67" 

上什麼IM任何想法做錯了什麼? 謝謝!

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; 
    } 
} 

} ?>

+0

什麼是mysqliConn類? –

+0

mysqliConn是連接類 –

+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; 
} 
+0

oki剛剛給我一秒試一下,謝謝:) –

+0

是非常感謝你,現在它的工作正常,$ lft和$ rgt得到正確的值,並沒有錯誤顯示 :) :) –

+0

嗯解釋.. – Saty

相關問題