0

I received some help moments ago, but was never able to find out why this query is not working? How might I modify this to make it work?

class Posts {

  public static function multipleQuery() {

    $result = mysql_query("SELECT * FROM posts ORDER BY id DESC LIMIT 0, 3")
              , __CLASS__);

      while($object = mysql_fetch_object($result)) {
        $return[] = $object;
      }
      return $return;
  }
}


$array = Posts::multipleQuery();
foreach($array AS $row) {
   echo $row->title;
}

Unfortunately I am not getting anything back. The query works, I have tested that out.

7
  • Try var_dump($array). Also , __CLASS__) is misplaced. Please be more specific on what exactly doesn't work. Commented Mar 18, 2011 at 16:52
  • What happens, what doesn't work? EIther way, this looks wrong ), __CLASS__); Commented Mar 18, 2011 at 16:52
  • 1
    Are you missing the database connection or is it just not part of this snippet? Commented Mar 18, 2011 at 16:53
  • ini_set('error_reporting', E_ALL); and ini_set('display_errors', true); set at the top of your script will ensure that errors are displayed and should help you to debug this. Commented Mar 18, 2011 at 16:56
  • 1
    Looking at the code, you have invalid syntax on line 3. Remove , __CLASS__). Commented Mar 18, 2011 at 16:56

2 Answers 2

4

The second argument on mysql_query should be a mysql connection handle. Passing your classe's name is not such a handle, so you're trying to execute the query on a database connection which doesn't exist. As well, you have no error checking, which would've shown this problem:

public static function multipleQuery(){
    $result = mysql_query("SELECT * FROM posts ORDER BY id DESC LIMIT 0, 3") 
              or die(mysql_error());

    while($object = mysql_fetch_object($result)) {
        $return[] = $object;
    }
    return $return;
}
Sign up to request clarification or add additional context in comments.

Comments

1

I'm guessing you have error reporting turned off, and are thus not seeing the parse error that should result from this line:

$result = mysql_query("SELECT * FROM posts ORDER BY id DESC LIMIT 0, 3"), __CLASS__);

I see two parentheses closed but only one opened.

Also you shouldn't throw the class name in there anyway. If anything, you should put in the object for the database connection, not the name of the class.

Comments

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.