Jump to content

PHP OOP ERROR


akaghachinaka

Recommended Posts

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"

Link to comment
Share on other sites

Here is my code

 

 

<?php

class 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();

?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

 

<?php

class 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 server
function set_host($path){
    $this->host = $path;
}

//set host server
function set_con($con){
    $this->con = $con;
}

//set host user parameters
function set_user($n, $p){
    $this->username = $n;
    $this->password = $p;
}

//set host database
function set_db($db){
    $this->db_name = $db;
}

//set slq string
function set_sqlstr($str){
    $this->sqlstr = $str;
}

//set slq result
function set_result($str){

    $this->result = $str;
}

//initialise database
function database(){
    $this->host = 'localhost';
    $this->username = 'root';
    $this->password = '';
    $this->con = '';
    $this->db_name = 'hot';
    $this->sqlstr = '';
    $this->status = '';
    $this->data = '';
}

//initialise database
function 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 database
function 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 database
function 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 DB
function 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 DB
function 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

Link to comment
Share on other sites

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.