akaghachinaka Posted August 10, 2015 Share Posted August 10, 2015 Hello guys i am new to OOP PHP. I am comfortable with PHP but i recently decided to step up my game by switching to OOP PHP. Now after learning some stuffs OOP I decided to create a class that connects to database, and finally does the query. now after creating an instance of the class and running a query i get this error "Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, string given" "Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, string given in" Quote Link to comment Share on other sites More sharing options...
scootstah Posted August 11, 2015 Share Posted August 11, 2015 Post your code please. Quote Link to comment Share on other sites More sharing options...
akaghachinaka Posted August 11, 2015 Author Share Posted August 11, 2015 Here is my code <?phpclass my_login_class { /* public $host = "localhost"; public $username = "root"; public $host_password = ""; public $database_name = "cndesk"; public $query = ""; protected $signup_error = ""; protected $signup_error2 = ""; protected $signup_success = ""; protected $login_error = ""; protected $login_success = ""; public $sql_connect = ""; public $sql_query = ""; */ var $host = "localhost"; var $username = "root"; var $host_password = ""; var $database_name = "cndesk"; var $query = ""; var $signup_error = ""; var $signup_error2 = ""; var $signup_success = ""; var $login_error = ""; var $login_success = ""; var $sql_connect = ""; var $sql_query = ""; public function sql_connect(){ $sql_connect = mysqli_connect($this->host,$this->username,$this->host_password); } public function sql_select_db(){ mysqli_select_db(mysqli_connect($this->host,$this->username,$this->host_password), $this->database_name); } public function set_sql_query($query){ $this->query = $query; } function get_sql_query(){ $this->sql_connect(); $this->sql_select_db(); $sql_query = mysqli_query(mysqli_connect($this->host,$this->username,$this->host_password),$this->query); $row = mysqli_fetch_assoc($this->sql_query); $num_row = mysqli_num_rows($this->sql_query); if($num_row==0){ echo'Sorry no posible match!'; } } function user_signup($signup_error){ $this->signup_error = $signup_error; echo''.$this->signup_error.''; }} $new_object = new my_login_class; $new_object->set_sql_query("SELECT * FROM `online` WHERE `user_login`='user1'"); $new_object->get_sql_query();?> Quote Link to comment Share on other sites More sharing options...
scootstah Posted August 11, 2015 Share Posted August 11, 2015 You have many wrong things going on in that. 1. $sql_query = mysqli_query(mysqli_connect($this->host,$this->username,$this->host_password),$this->query); $row = mysqli_fetch_assoc($this->sql_query); $num_row = mysqli_num_rows($this->sql_query);You are creating a local variable $sql_query, but then trying to use a class property $this->sql_query. Pick one or the other. 2. That class should not be responsible for connecting to the database, and shouldn't be connecting multiple times. You should pass in an already established connection that happens only once in your application. class my_login_class { private $db; public function __construct($db) { $this->db = $db; } } $db = new mysqli('localhost', 'root', '', 'cndesk'); // notice we have no mysqli_select_db - that's because // the database name is the 4th argument in the constructor $login = new my_login_class($db); 3.Your method names are a bit weird and inconsistent. In set_sql_query(), you are setting a class var but then in get_sql_query() you are executing, and getting results for, the query that you set previously. With this naming convention one would assume that get_sql_query() simply returns the query that you gave to set_sql_query(). 4. What is user_signup() supposed to be doing? 5. Make sure to always include visibility keywords on properties and methods, for clarity. Instead of function get_sql_query(){, it should be public function get_sql_query(){, like you do elsewhere. 6. Using var on class properties has been frowned upon for a long time now. You should use public, protected, or private accordingly. 7. The get_sql_query() method should not know what to do when the query has no results. It should simply tell the calling code such, and let the calling code figure it out. This method should not have that responsibility. Hope that helps. Quote Link to comment Share on other sites More sharing options...
akaghachinaka Posted August 12, 2015 Author Share Posted August 12, 2015 Thanks scootstah Quote Link to comment Share on other sites More sharing options...
akaghachinaka Posted August 12, 2015 Author Share Posted August 12, 2015 But why do i have to create a separate class for connection. Here is a code i got online that works and a separate class wasnt created forconnection <?phpclass database{var $host;var $username;var $password;var $con;var $db_name;var $sqlstr;var $result;var $data;var $status;var $no_rows;//set host serverfunction set_host($path){ $this->host = $path;}//set host serverfunction set_con($con){ $this->con = $con;}//set host user parametersfunction set_user($n, $p){ $this->username = $n; $this->password = $p;}//set host databasefunction set_db($db){ $this->db_name = $db;}//set slq stringfunction set_sqlstr($str){ $this->sqlstr = $str;}//set slq resultfunction set_result($str){ $this->result = $str;}//initialise databasefunction database(){ $this->host = 'localhost'; $this->username = 'root'; $this->password = ''; $this->con = ''; $this->db_name = 'hot'; $this->sqlstr = ''; $this->status = ''; $this->data = '';}//initialise databasefunction reset(){ $this->host = 'localhost'; $this->username = 'root'; $this->password = ''; $this->con = ''; $this->db_name = 'hot'; $this->sqlstr = ''; $this->status = ''; $this->data = '';}//function to connect to the databasefunction connect(){ $conn = mysql_connect($this->host,$this->username,$this->password); if(!$conn) echo ("couldn't connect"); else $this->set_con($conn);}//function to close connection to the databasefunction close_connection(){ $conn= $this->con; mysql_close($conn);}//method to create and connect to a DB function create_db($dummy_db_name){ $this->connect(); $this->sqlstr = "CREATE DATABASE ". $dummy_db_name ; $query = mysql_query($this->sqlstr) ; if(!$query) echo ("create database error"); else { $this->set_db($dummy_db_name); $this->close_connection(); } } //method to perform scalar operation function ex_scalar(){ $this->status = 0; $this->connect(); $query = mysql_select_db($this->db_name) ; if(!$query) echo ("Couldn't select DB") ; else{ $query = mysql_query($this->sqlstr); if(!$query) echo ("scalar error"); else{ $this->status = 1; $this->close_connection(); } } } // method to query the DBfunction querydata(){ $this->status = 0; $this->connect(); $query = mysql_select_db($this->db_name) ; if(!$query) echo ("could not select DB"); else $resultset = mysql_query($this->sqlstr); if(!$resultset) echo ("could not execute sql query"); else{ $this->no_rows = mysql_num_rows($resultset); $this->set_result($resultset); $this->fetchdata(); $this->status = 1; $this->close_connection(); }}// method to query the DBfunction fetchdata(){ $this->data = mysql_fetch_array($this->result); }}?> I decided to write a new one from scratch because like i said i just started learning OOP Quote Link to comment Share on other sites More sharing options...
scootstah Posted August 12, 2015 Share Posted August 12, 2015 Just because you found it online doesn't mean that it is good. That code is very old and outdated as well. You're not creating a separate class for database connection. PHP comes with the mysqli class already, you don't have to create that. Quote Link to comment Share on other sites More sharing options...
gizmola Posted August 15, 2015 Share Posted August 15, 2015 How many more posts will you make without using code tags for readability of your code? The first reply from scootstah used them. One would think that would be a hint you might look into. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.