Jump to content

Recommended Posts

Hello everyone,

 

For two weeks now, I'm trying to get this database connection in my query. Can someone give me a solution and tell me what I've done wrong? Am I overlooking something?

 

<?php

class Mysql{
public function connect(){
	$mysqli = new mysqli('localhost','root','','login');
}
}

class Query extends Mysql{
public function runQuery(){
	$this->result = parent::connect()->query("select bla bla from bla bla");
}
}

$query = new Query;
$query->runQuery();

?>

Link to comment
https://forums.phpfreaks.com/topic/226868-oo-php-plus-oo-mysqli/
Share on other sites

You are not returning or saving the connection:

 

class DbMySQLi {
    private $connection;
    
    public function connect() {
        if(is_null($this->connection)) {
            $this->connection = new mysqli('localhost', 'root', '', 'login');
        }
        return $this->connection;
    }
}

class Query extends DbMySQLi {
    private $result;
    public function runQuery() {
        $this->result = $this->connect()->query('SELECT * FROM bla');
    }
}

$query = new Query();
$query->runQuery();

Thanks!

 

So I always have to return the value that i have created for another variable?

 

No, a class is a container of variables and functions. I could establish the DB connection in the constructor and all methods would have access to the DB connection.

Thanks!

 

So I always have to return the value that i have created for another variable?

 

No, a class is a container of variables and functions. I could establish the DB connection in the constructor and all methods would have access to the DB connection.

 

Then why is it necessary in this case? (sorry for the lame questions)

Then why is it necessary in this case? (sorry for the lame questions)

 

It isn't, I just extended upon your example.

 

Ah ok.

 

But i don't want to store all methods in one class because then it is not object oriented right? So, why is this necessary in my example?

Ok,

 

Thankyou for your patience and solving my problem.

 

I still have one question open :). Why do i have to return the value in the example given?

 

    public function connect() {
        if(is_null($this->connection)) {
            $this->connection = new mysqli('localhost', 'root', '', 'login');
        }
        return $this->connection;

Because in your code you call:

 

$this->connect()->query('SELECT * FROM bla');

 

If you don't return the value the above wouldn't work. You could rewrite it making the connection in the constructor or something and making the connection protected and call it like:

 

$this->connection->query('SELECT * FROM bla');

 

Your code would look then like:

 

class DbMySQLi {
    protected $connection;
    
    public function connect() {
        $this->connection = new mysqli('localhost', 'root', '', 'login');
    }
}

class Query extends DbMySQLi {
    private $result;
    public function runQuery() {
        $this->result = $this->connection->query('SELECT * FROM bla');
    }
}

I forgot to add the constructor, here it is again:

 

class DbMySQLi {
    protected $connection;
    
    public function __construct() {
        $this->connect();
    }
    
    public function connect() {
        $this->connection = new mysqli('localhost', 'root', '', 'login');
    }
}

class Query extends DbMySQLi {
    private $result;
    public function runQuery() {
        $this->result = $this->connection->query('SELECT * FROM bla');
    }
}

I have marked this problem solved. But i hope you still want to answer my last question.

 

Why do i have to return returnQuery() ? And why don't i have to return runQuery() ?

 

<?php

class Mysql{

protected $db;

public function __construct(){
	$this->db = new mysqli('localhost','root','','login');
}
}

class Query extends Mysql{

private $result;

public function runQuery($query){
	$this->result = $this->db->query($query);
}
public function returnQuery(){
	return $this->result->num_rows;
}
}

class User{

private $query; // Instantiate Query object.

public function __construct(){
	$this->query = new Query;
}
public function login(){
	$this->setQuery = "select userid from users where username = 'wouter' and password = 'test'";
	$this->query->runQuery($this->setQuery);

	if($this->query->returnQuery() > 0){
		echo "You are logged in!";
	}else{
		echo "Wrong username / password!";
	}
}
}

?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.