Jump to content

akaghachinaka

New Members
  • Posts

    8
  • Joined

  • Last visited

akaghachinaka's Achievements

Newbie

Newbie (1/5)

0

Reputation

  1. 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
  2. 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(); ?>
  3. 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"
×
×
  • 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.