1

I am new to the idea of oop php and i am trying to write an irc php.

What I'm trying to do: I am trying to query my database, get results from my database and put it into an array inside my program.

I tried making a new function to carry out the task and called it in the __construct function.

I have shortened the code but it pretty much looks like this:

Any thoughts and ideas are much appreciated.

class IRCBot
{    

    public $array = array();
    public $servername = "localhost";
    public $username = "root";
    public $password = "usbw";
    public $dbname = "bot";


function __construct()
{
    //create new instance of mysql connection
    $conn = new mysqli($this->servername, $this->username, $this->password, $this->dbname); 

    if ($mysqli->connect_errno) 
    {
        echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
    }
    echo $mysqli->host_info . "\n";

    $this->database_fetch();

}

function database_fetch()
{
    $query = "SELECT word FROM timeoutwords";
    $result = mysqli_query($query);
    while($row = mysqli_fetch_assoc($result))
        {
            $array[] = $row();
        }
}

function main()
{
    print_r($array);
}



}

$bot = new IRCBot();
8
  • pass $conn into $this->database_fetch(); as $this->database_fetch($conn); because mysqli_query need ist param as a link identifier but this will not work Commented Oct 28, 2016 at 10:58
  • make it property $conn as $this->conn than get this property into main method and call database_fetch() inside the main() method not in constructor Commented Oct 28, 2016 at 11:00
  • 1
    3 words: "check for errors". Commented Oct 28, 2016 at 11:19
  • 1
    Amazing what "three little words" will do to someone's day, eh? @devpro Commented Oct 28, 2016 at 11:27
  • 1
    So @Reece, Have you implemented the answer given below? Commented Oct 28, 2016 at 18:10

2 Answers 2

3

Changes

1) Change if ($mysqli->connect_errno) to if ($conn->connect_errno)

2) Change $array[] = $row(); to $array[] = $row;

3) Add return $array; in function database_fetch()

4) Call database_fetch() function inside main() function instead of constructor.

5) Add $this->conn in mysqli_query() (Thanks @devpro for pointing out.)

Updated Code

<?php
class IRCBot
{    

    public $array = array();
    public $servername = "localhost";
    public $username = "root";
    public $password = "usbw";
    public $dbname = "bot";
    public $conn;


    function __construct()
    {
        //create new instance of mysql connection
        $this->conn = new mysqli($this->servername, $this->username, $this->password, $this->dbname); 

        if ($this->conn->connect_errno) 
        {
            echo "Failed to connect to MySQL: (" . $this->conn->connect_errno . ") " . $this->conn->connect_error;
        }

    }

    function database_fetch()
    {
        $query = "SELECT word FROM timeoutwords";
        $result = mysqli_query($this->conn,$query);
        while($row = mysqli_fetch_assoc($result)){
            $array[] = $row;
        }
        return $array;
    }

    function main()
    {
        $data = $this->database_fetch();
        print_r($data);
    }

}

Quick Start

Sign up to request clarification or add additional context in comments.

3 Comments

friend, u missed the very important mysqli_query($query) ist param
now looks fine friend, just one suggestion u must need to initialize array $array = array();
I think, no need as in top public $array = array(); is initialised @devpro
3

First of all you need to fix error from your constructor, you can modify as:

function __construct()
{
    //create new instance of mysql connection
    $this->conn = new mysqli($this->servername, $this->username, $this->password, $this->dbname); 

    if ($this->conn->connect_errno) 
    {
        echo "Failed to connect to MySQL: (" . $this->conn->connect_errno . ") " . $this->conn->connect_error;
    }
    echo $this->conn->host_info . "\n";
}
  • Here, you need to replace $mysqli with $conn because your link identifier is $conn not $mysqli
  • No need to call database_fetch() here.
  • You need to use $conn as a property.

Now you need to modify database_fetch() method as:

function database_fetch()
{
    $query = "SELECT word FROM timeoutwords";
    $result = mysqli_query($this->conn,$query);
    $array = array();
    while($row = mysqli_fetch_assoc($result))
    {
        $array[] = $row;
    }
    return $array;
}
  • Here, you need to pass add first param in mysqli_query() which should be link identifier / database connection.

  • Second, you need to use return for getting result from this function.

In last, you need to modify your main() method as:

function main()
{
    $data = $this->database_fetch();
    print_r($data);
}
  • Here, you need to call database_fetch() method here and than print the data where you need.

5 Comments

I think $array[] = $row(); will have problem here?
@NanaPartykar: :) opps
Sometimes happen. But, that was super for me $_POST('pedert').
@NanaPartykar: hahaha, actually something like shoro shoro k din Early days
Ha Ha :D Shuru Shuru Ke Din, Wo Bhi Kya Din They.. Let's Jump To Other Question Too. @devpro

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.