0

New to classes.

public function get_feed($member_id) {



    $sql = "SELECT mc. * , cp. * 
            FROM `my_connections` mc
            LEFT JOIN `thoughts` cp ON cp.`member_id` = mc.`connection_id` 
            WHERE mc.`member_id` = '".$member_id."'
            AND mc.`approved` = 1";

    $result = @mysql_query($sql,$db); check_sql(mysql_error(), $sql, 0);

    //echo '<pre>'.$sql.'</pre>';

            return $result; //this returns Resource id#14 or NULL


    /*while($list = mysql_fetch_array($result)) {

        $feed_item[] = $list;
    }

    return $feed_item;*/ 

}// EO get_feed()

when I call it

$feed = new my_feed;

echo $feed->get_feed($uid);

it returns Null.

But when I use the same sql outside the method it returns expected result.

what should I be returning in a query like above.

Angad Dubey
  • 5,067
  • 7
  • 30
  • 51
  • 1
    $db looks like your connection string and you are using inside the function so you may need to use as global $db ; inside the function before using it. – Abhik Chakraborty Jan 12 '14 at 19:44
  • 1
    $db? looks like a scope issue –  Jan 12 '14 at 19:44
  • 3
    Perhaps if you didn't suppress errors on your `mysql_query` call you would see why it's failing. – jszobody Jan 12 '14 at 19:46
  • You are both correct, I missed that- Still getting the same result though when returning $result, i.e. Resource id #14 – Angad Dubey Jan 12 '14 at 19:47
  • 2
    @maximl337 `mysql_query` returns a resource. That's how it is supposed to work. What are you expecting? See http://us2.php.net/mysql_query – jszobody Jan 12 '14 at 19:47
  • 2
    @AbhikChakraborty `global` shouldn't be used in OOP. Better to use `$this->db` or similar. – user555 Jan 12 '14 at 19:48
  • @jszobody You are correct, what I want to know is, is it proper to just return $result then loop through it or to loop through it with a while loop and put the resources in an array and return that (like in the commented section of the code) – Angad Dubey Jan 12 '14 at 19:49
  • Yes if your class has the $db access or if its a part of its parent class – Abhik Chakraborty Jan 12 '14 at 19:50
  • @maximl337 That's entirely up to you. That's a design choice. – jszobody Jan 12 '14 at 19:50
  • Also see http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php?rq=1 – MECU Jan 12 '14 at 19:52
  • And while you're at it stop using `mysql_*` and use `mysqli` or `PDO`. See http://www.phptherightway.com/#databases. – user555 Jan 12 '14 at 19:52
  • I have inherited a legacy application, I am slowly modernizing it, will make the changes soon.thank you – Angad Dubey Jan 12 '14 at 19:53
  • @jszobody anyway I can mark your answer as complete through comments. sry fairly new to SO aswell. – Angad Dubey Jan 12 '14 at 19:54

2 Answers2

3

Several things:

  1. You are suppressing errors, leaving yourself in the dark:

    $result = @mysql_query($sql,$db); // Remove the @ symbol

  2. You should then find that $db isn't a valid database connection object, due to scope. Make sure you have it setup as a class variable, then reference $this->db instead.

  3. Your Resource id#14 result is expected, that's what mysql_query returns. You can use the various mysql_fetch_* methods to parse it here, or simply return it and let the caller handle parsing.

  4. Stop using mysql_* methods! Switch to mysqli or PDO as soon as you can (I understand that can take time with legacy applications).

jszobody
  • 28,495
  • 6
  • 61
  • 72
1

$result always return Resource id#14 is the way php handle that, to get the data you have to fetch it using the functions php have for that, like mysql_fetch_array or mysql_fetch_assoc

$result = @mysql_query($sql,$db); check_sql(mysql_error(), $sql, 0);

while($list = mysql_fetch_array($result)) {

    $feed_item[] = $list;
}

return $feed_item;

$feed _item will have the query result into an array

NOTE: Why shouldn't I use mysql_* functions in PHP?

Try to avoid suppressing errors like the @ in @mysqli_query you will need get the error to debug

Community
  • 1
  • 1
Emilio Gort
  • 3,475
  • 3
  • 29
  • 44