Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
204 views
in Technique[技术] by (71.8m points)

I want to display table rows in same class and difference function in PHP

how to display rows in my users table in PHP arr() function can't connect with database

class DB {
    protected $db_server = 'localhost';
    protected $db_username = 'root';
    protected $db_password = 'root';
    protected $db_name = 'dbname';
    public function connect() {
        $connect_db = new mysqli($this->db_server, $this->db_username, $this->db_password, $this->db_name);
        if(mysqli_connect_errno()) {
            printf("Connection failed!", mysqli_connect_error());
            exit();
        } else {
            echo "connected";
        }
        return true;
    }
    function arr() {
        $conn = $this->connect();
        if($query = mysqli_query($conn, "SELECT * FROM users")) {
            echo 'row is ' . mysqli_num_rows($query);
        }
    }
}
$db = new DB();
$db->arr();

result -> "connected row is " not count rows


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

In the connect() function, change

return true;

to

return $connect_db;

so that it returns the connection object. Then you can use it in your other functions.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...