0

My MySQL table looks like this:

MySQL Table

and I want to retrieve the data from the column 'host' from where uid is equal to twenty.

I have some code that will connect to the database and will return the row when uid is equal to twenty

DB.php:

public function query($sql, $params = array()){
    $this->_error = false;
    if($this->_query = $this->_pdo->prepare($sql)){
        $x = 1;
        if(count($params)){
            foreach($params as $param){
                $this->_query->bindValue($x, $param);
                $x++;
            }
        }

        if($this->_query->execute()){
            $this->_results = $this->_query->fetchAll(PDO::FETCH_OBJ);
            $this->_count = $this->_query->rowCount();
        } else {
            $this->_error = true;
        }
    }

    return $this;
}

public function results(){
    return $this->_results;
}

server.php:

public static function get($uid){
    $_db = DB::getInstance();
    $data = $_db->get('servers', array('uid', '=', $uid));

    return $data->results();
}

cURL.php:

$a = Server::get('20');
var_dump($a);

The var_dump on the return gives the following: PHP var_dump return

array(2) { [0]=> object(stdClass)#5 (4) { ["sid"]=> string(2) "13" ["host"]=> string(12) "example.home" ["port"]=> string(4) "8081" ["uid"]=> string(2) "20" } [1]=> object(stdClass)#6 (4) { ["sid"]=> string(3) "153" ["host"]=> string(15) "stream.as.ag.ca" ["port"]=> string(5) "23434" ["uid"]=> string(2) "20" } }

Note: If this question has already been answered somewhere else, could a link be provided as I couldn't find the answer.

2
  • Actually I zoomed in so nevermind, might be useful for other users to be able to see it though. The data is there are you having issues iterating through it? Look into foreach. Commented May 29, 2015 at 18:15
  • Yes, I have issues on how to go through the data and then extract the 'host'. Commented May 29, 2015 at 18:16

1 Answer 1

1

I think you should be able to pull each host with this.

foreach($a as $row) {
    echo $row->host;
}
Sign up to request clarification or add additional context in comments.

3 Comments

It's returning the following error: Catchable fatal error: Object of class stdClass could not be converted to string in cURL.php at the line echo $data
Hmm haven't used the FETCH_OBJ before, how about this update?
what are you referring to?

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.