0

I developed on Java Swing OOP so I am a little confused with a PHP OOP Techniques, they are the same in logic but not the same with syntax. Please help me with this, I need your specialties. Thank you all.

this is my index.php

<?php
require_once("DB.class.php");

$db = new DB("localhost", "root", "100510", "findersa_jfph_smart");

$db->select("t1.`sid` AS 'id', t1.`Title` AS 'title', t1.`JobDescription` AS 'content', t1.`Location_City` AS 'city', t1.`SalaryType` AS 'salary',
 t1.`EmploymentType` AS 'working_hours', t2.`CompanyName` AS 'company', t1.`JobRequirements` AS 'requirements', t1.`JobCategory` AS 'category', t1.`activation_date` AS 'date',
 t2.`PhoneNumber` AS 'contact_telephone'","`listings` AS t1
LEFT JOIN `users` AS t2
ON t1.`user_sid` = t2.`sid`","t1.`activation_date` BETWEEN '2016-02-02 00:00:00' AND '2016-05-02 23:59:59' AND t1.active = '1' AND t1.listing_type_sid = '6'
LIMIT 50000");

?>

this is my DB.class.php

<?php
class DB {

    public $host;
    public $user;
    public $pass;
    public $dbase;

    public function __construct($host, $user, $pass, $dbase) {

        $this->host = $host;
        $this->user = $user;
        $this->pass = $pass;
        $this->dbase = $dbase;

        $conn = new mysqli($this->host, $this->user, $this->pass, $this->dbase);

        if ($conn->connect_error) {
            die("Connection failed: " . $conn->connect_error);
        }

         echo "Connected Successfully!";
    }

    public function select($myCols, $myTable, $myQuery) {

        if (!isset($myQuery)) {
            $sql = "select " . $myCols . " from " . $myTable;
        } else {
            $sql = "select " . $myCols . " from " . $myTable . " from " . $myQuery;
        }

        $result = $this->conn->query($sql);

        if ($result->num_rows > 0) {
            return $result;
        } else {
            return "0 results";
        }

        $conn->close();
    }

}


?>

Sorry I can't get all the code in the box. But this is where i have a particular error:

$result = $this->conn->query($sql);

in DB.class.php.

Thank you. This is my first question here, please bear with me.

chris85
  • 23,846
  • 7
  • 34
  • 51

1 Answers1

1

In PHP declaring the variable in the class definition is not a requirement, it's more for readability. Although you can set the definition of public, protected, private and static.

You can assign variables to the class level by making references with the $this instance. If you're writing it as a static class you would make references with self instead.

With your current code the database connection is not available in the select method, this can be easily achieved by assigning it to the class level via $this->conn = new mysqli();. When a variables is defined as $conn = new mysqli(); it's only accessible to the method that declares it.

Bankzilla
  • 2,086
  • 3
  • 25
  • 52
  • If you need more understanding, [this question describes how the class hierarchy and references work](http://stackoverflow.com/a/21902271/970057) – Bankzilla May 10 '16 at 03:57