php oop和mysql查询

Chr*_*ris 3 php mysql oop variables

我正在尝试创建一个查询数组的函数,然后我将能够调用单独的值.在这里,我想你会理解我想要做的事情.除了我是一个完整的初学者.

class Posts{

  var $title;
  var $author;

  public function querySinglePost($value){

    $post = mysql_fetch_array(mysql_query("select * from posts where id=$value"));  
    $this->title = $post['title'];
    $this->author = $post['author'];

  } 


}
Run Code Online (Sandbox Code Playgroud)

如何将数组值分配给类中的变量,然后在我的普通php脚本/视图中调用它们?谢谢

Sla*_*ava 5

看看mysql_fetch_object.我还建议将该函数设为static,它将返回创建的对象.像这样:

class Posts{

  var $title;
  var $author;

  public static function querySinglePost($value){

    return mysql_fetch_object(
       mysql_query("select * from posts where id=$value"),
       __CLASS__);

  } 

}

$post = Posts::querySinglePost($value);
$a = $post->title;
//...
Run Code Online (Sandbox Code Playgroud)