开发者

php oop mysql query

开发者 https://www.devze.com 2023-02-18 05:14 出处:网络
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?

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("S开发者_Go百科ELECT * 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.


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;
}


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消