0
Hello, I’m in trouble, I have some queries to do, I wanted to leave within class, I’m making the following code:
<?php
class MySQL {
private $user;
private $password;
private $database;
private $host;
public function __construct($user, $password, $database, $host="localhost"){
$this->user = $user;
$this->password = $password;
$this->database = $database;
$this->host = $host;
$this->mysqli = new mysqli($this->host, $this->user, $this->password, $this->database);
if (mysqli_connect_errno()) {
die('FATAL ERROR: Can not connect to SQL Server.');
exit();
}
}
public function _query($qr)
{
$this->result = $this->mysqli->query($qr);
return $this->result;
}
public function _close()
{
$this->mysqli->close();
}
}
To call, I do:
<?php
session_start();
$connect = new MySQL(UserMySQL, PassMySQL, DataBaseMySQL, ServerMySQL);
function test(){
$search = $connect->_query("SELECT value FROM tab2 WHERE type='".$type."'");
$search = $connect->_query("SELECT * FROM tab1 WHERE urlName='".$subName."'");
}
test();
But returns me error. Call to a Member Function _query() on a non-object in ..........
From the error message, it seems that the connection failed, from a
var_dump($connect);
– rray
@rray Object(Mysql)#1 (5) { ["user":"Mysql":private]=> string(15) "user" ["password":"Mysql":private]=> string(12) "password" ["database":"Mysql":private]=> string(15) "database" ["host":"Mysql":private]=> string(9) "localhost" ["mysqli"]=> Object(mysqli)#2 (19) { ["affected_rows"]=> int(0) ["client_info"]=> string(6) "5.5.49" ["client_version"]=> int(50549) ["connect_errno"]=> int(0) ["connect_error"]=> NULL ["Errno"]=> int(0) [""""]=> string(0) "["error_list"]=> array(0) { } ["field_count"]=> int(0) ["host_info"]=>string(25) "Localhost via UNIX socket" ["info"]=> .....................
– Leonardo Joksan
@rray ............................ NULL ["insert_id"]=> int(0) ["server_info"]=> string(10) "5.5.50-cll" ["server_version"]=> int(50550) ["stat"]=> string(155) "Uptime: 415835 Threads: 13 Questions: 1647446872 Slow queries: 178 Opens: 3500320 Flush Tables: 1 Open Tables: 2750 Queries per Second avg: 3961.780" ["sqlstate"]=> string(5) "00000" ["protocol_version"]=> int(10) ["thread_id"]=> int(6998409) ["warning_count"]=> int(0) } }
– Leonardo Joksan
And I agree that the problem may be in connection. And to make it easier to detect errors, it would be better to leave only the instance of variables. Create a connection-only method and call it as soon as you instantiate the class.
– Débora Castro
Copy, paste, changed database and query data and returned no error, the problem seems to be elsewhere.
– rray
The test you did was the one you added to the edit? Because it won’t work that way
– rray
@rray and Deborah thank you very much for the tips, I managed to solve, and I have published explaining, thank you very much!!!!!
– Leonardo Joksan
So it’s nice to always post the real code, sometimes hide some details in the question make it difficult to detect the problem.
– rray
@rray Truth... living and learning right? I won’t do it again.
– Leonardo Joksan