简体   繁体   English

使用OOP mysqli扩展类与数据库连接

[英]connection with database with OOP mysqli extending class

I know must coding part of mysql bt new at mysqli. 我知道必须在mysqli上对mysql bt new的一部分进行编码。 I am not able to execute these insert query to the database. 我无法对数据库执行这些插入查询。 I have searched a lot but couldn't find simple suggestion, or may be i didn't understand. 我进行了很多搜索,但找不到简单的建议,或者可能是我听不懂。

Undefined variable: mysqli in C:\\wamp\\www\\New folder\\php\\msqliconnect.php on line 32 未定义变量:第32行上C:\\ wamp \\ www \\ New folder \\ php \\ msqliconnect.php中的mysqli

Fatal error: Call to a member function mysqli_query() on a non-object in C:\\wamp\\www\\New folder\\php\\msqliconnect.php on line 32 致命错误:在第32行的C:\\ wamp \\ www \\ New folder \\ php \\ msqliconnect.php中的非对象上调用成员函数mysqli_query()

Any help is appreciated. 任何帮助表示赞赏。

<?php
class connection

    {
    public $mysqli;

    function connect()
        {
        $hostname = "localhost";
        $username = "root";
        $password = "";
        $database = "demodatabase";
        $mysqli = new mysqli($hostname, $username, $password, $database);
        /* check connection */
        if (mysqli_connect_errno())
            {
            printf("Connect failed: %s\n", mysqli_connect_error());
            exit();
            }

        return true;
        }
    }

class Index extends connection

    {
    function __construct()
        {
        parent::connect();
        }

    function insertdata($a, $b)
        {

        // echo $a. ' ' .$b;
        // MySqli Insert Query

        $status = 0;
        $insert_row = $mysqli->mysqli_query("INSERT INTO tb_user (id, user, password, status) VALUES('','" . $a . "', '" . $b . "', '')");
        if ($insert_row)
            {
            print 'saved';
            }
          else
            {
            die('Error : (' . $mysqli->errno . ') ' . $mysqli->error);
            }
        }
    }

?>

In both of your connect() and insertdata() methods, you're using local variable $mysqli , not the instance variable public $mysqli; connect()insertdata()方法中,都使用局部变量 $mysqli ,而不是实例变量 public $mysqli; . You should use $this->mysqli instead of $mysqli in your instance methods. 您应该在实例方法中使用$this->mysqli而不是$mysqli So your connect() and insertdata() methods would be like this: 因此,您的connect()insertdata()方法将如下所示:

function connect(){
    $hostname = "localhost";
    $username = "root";
    $password = "";
    $database = "demodatabase";
    $this->mysqli = new mysqli($hostname, $username, $password, $database);
    /* check connection */
    if (mysqli_connect_errno()) {
        printf("Connect failed: %s\n", mysqli_connect_error());
        exit();
    }
    return true;            
}

and

function insertdata($a, $b){
    $insert_row = $this->mysqli->query("INSERT INTO tb_user (id, user, password, status) VALUES('','".$a."', '".$b."', '')");
    if($insert_row){
        print 'saved'; 
    }else{
        die('Error : ('. $this->mysqli->errno .') '. $this->mysqli->error);
    }
}

Sidenote: Learn about prepared statement because right now your query is susceptible to SQL injection attack. 旁注:了解准备好的语句,因为现在您的查询容易受到SQL注入攻击。 Also see how you can prevent SQL injection in PHP . 另请参阅如何防止在PHP中进行SQL注入

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM