dsxml2169 2012-06-13 08:04 采纳率: 0%
浏览 35
已采纳

PHP MySQLi对象声明和检索

I've got a couple of model classes handling a db layer, and I've got a logic error in how to hand the db connection to the query in an OO way. Class1 is a db_connect which I'm calling inside the constructor of class2 query methods:

class db {
    public $host = 'localhost';
    public $username = 'root';
    public $password = '';
    public $database = 'molecule';
    public $mysqli = '';

   function __construct() {
        $mysqli = new mysqli($this->host, $this->username, $this->password, $this->database);
            if (mysqli_connect_errno()) {
                printf("Database Connection failed: %s
", mysqli_connect_error());
                exit();
            }
        return $mysqli;
    }   
}

class nodeModel {
    public $node;
    public $node_name;
    public $node_link;
    public $node_comment;
    public $mysqli;

    function __construct() {
        $mysqli = new db;
        return $mysqli;
    }

    public $insert;
    public $insert_id;
    function insertNode($node_name, $node_link, $node_comment) {
            $this->insert = $insert;
            $this->insert = $mysqli->prepare("INSERT INTO node(node_name, name_link, node_comment) VALUES (?, ?, ?)");
            $this->insert->bind_param("sss", $this->node_name, $this->node_link, $this->node_comment);
            if($this->insert->execute()) {
                $this->insert_id = $mysqli->insert_id;
            } 
            return $this->insert_id;
            print_r($this->insert_id);
            $this->insert->close();
        }}

But I'm getting an undefined variable error with $insert.

I am calling that layer with the following:

include 'node_model.php';
$dbTest = new nodeModel;
$node_name = 'My Node Title';
$node_link = 'My Node Link';
$node_comment = 'My Node Comment. This one should be longer so I will write more stuff';
$dbTest->insertNode($node_name, $node_link, $node_comment);

The way I'm connecting with the constructor of the query class seems to be working but not sure which variable I need in scope to attach to the prepare/bind/execute statements?

And I do know that I should be using PDO or another ORMy type tool. Which I will, but this is more about some basic OO get/set/retrieve best practice ... thanks for your help.

  • 写回答

2条回答 默认 最新

  • doushih06137 2012-06-13 08:51
    关注

    There are some major problems with Your code...

    1. Make Your model class properties private and create setter and getter methods to set or retrieve a value to/from property. Also Your method insertNode is wrong.

    class nodeModel {
        private $node;
        private $nodeName;
        private $nodeLink;
        private $nodeComment;
        private $mysqli;
    
        function __construct() {
            $this->mysqli = new db;
        }
    
        public function getNodeName() {
            return $this->nodeName;
        }
    
        public function getNodeLink() {
            return $this->nodeLink;
        }
    
        public function getNodeComment() {
            return $this->nodeComment;
        }
    
        public function setNodeName($value) {
            $this->nodeName = $value;
        }
    
        public function setNodeLink($value) {
            $this->nodeLink = $value;
        }
    
        public function setNodeComment($value) {
            $this->nodeComment = $value;
        }
    
        function insertNode() {
            $this->insert = $this->mysqli->prepare("INSERT INTO node(node_name, node_link, node_comment) VALUES (?, ?, ?)");
            $this->insert->bind_param($this->node_name, $this->node_link, $this->node_comment);
            if($this->insert->execute()) {
                $this->insert_id = $this->mysqli->insert_id;
            }
            $this->insert->close();
    
            print_r($this->insert_id);
    
            return $this->insert_id;
        }}
    
    
    }
    

    2. In class db when You create a connection store it into $this->mysqli, not just $mysqli.

    function __construct() {
        $this->mysqli = new mysqli($this->host, $this->username, $this->password, $this->database);
            if (mysqli_connect_errno()) {
                printf("Database Connection failed: %s
    ", mysqli_connect_error());
                exit();
            }
        return $this->mysqli;
    }
    

    3. In Your third code, you create only a variables that You pass to the method insertNode but within this method You do nothing with the passed variables - You expect that the class properties are set but they are not... Therefore do this:

    include 'node_model.php';
    $dbTest = new nodeModel;
    $dbTest->setNodeName('My Node Title');
    $dbTest->setNodeLink('My Node Link');
    $dbTest->setNodeComment('My Node Comment. This one should be longer so I will write more stuff');
    $dbTest->insertNode();
    

    Hope this helps...

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度