D
D
Dmitry2014-03-19 21:24:30
PHP
Dmitry, 2014-03-19 21:24:30

How to query the database in this case?

db.php file:

require 'config.php';
/**
* 
*/
class dbConnect
{
  protected $table = '';


  function __construct($table)
  {
    $this->table = $table;
  }

  public function dbConnect() {
    $db = new mysqli (DB_HOST, DB_USER, DB_PASS) or die (' WRONG CONNECTION PARAMETERS');
    $db->select_db($this->table) or die (' WRONG TABLE');
    $db->set_charset('utf-8');
  }
}

?>

Connects to the database.
There is index.php, in which an instance of the class is created, and a connection to the base test :
<?php  
require 'db.php';
  #DATABASE CONNECT;
$testConnect = new dbConnect('test');
$testConnect->dbConnect();
?>

Now the question is how do I access the $db instance for a query?
Ideally, there should be $db->query, but, apparently, I'm stupid somewhere. Thanks

Answer the question

In order to leave comments, you need to log in

2 answer(s)
Y
Yuri Morozov, 2014-03-19
@another_dream

Or add something like return $db to dbConnect , and then, respectively, $db = $testConnect->dbConnect();
or

public $db = '';
...
$this->db = new mysqli(...
$testConnect->db->...

D
Dmitry, 2014-03-19
@another_dream

And another question immediately arose.
Made:

....
public function dbConnect() {
        ...
        ...
        return $db;
  }
}

and beyond
$db = $testConnect->dbConnect();
$sql = $db->query('SELECT name FROM qwert');
print_r($sql);

Why suddenly the SQL query returns:
mysqli_result Object ( [current_field] => 0 [field_count] => 1 [lengths] => [num_rows] => 1 [type] => 0 )

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question