使用mysqli获取数据

时间:2010-08-10 10:21:34

标签: php mysql oop mysqli

我一直在尝试mysqli。

使用程序化的mysql,代码显示结果很好。然而,在切换到OOP mysqli之后,我尝试将代码转换为mysqli,并且它说没有引号。 代码有什么问题吗?

/* the database object */
        private $_db;

        public function __construct($db=NULL)
        {
            if(is_object($db))
            {
                $this->_db = $db;
            }
            else
            {

                $this->_db = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
            }
        }

        public function displayQuotes()
        {
            $sql = "SELECT cQuotes, vAuthor, cArabic, vReference 
                          FROM thquotes 
                          ORDER BY RAND()
                          LIMIT 1;";

                  try {

                      $query = $this->_db->prepare($sql);
                      $query->execute();
                      $query->store_result();

                      /* bind variables to prepared statement */
                      $query->bind_result($cQuotes, $vAuthor, $cArabic, $vReference);


                      if(!$query->num_rows==0)
                      {
                        while($row = $query->fetch())
                        {
                            //echo $this->formatQuotes($row);
                            $formatHTML = new formatHTML();
                            echo $formatHTML->formatQuotes($row);
                        }


                      }
                      else
                         {
                            echo "There are no Quotes!";
                         }
                       $query->free_result();
                      $query->close();
                    }
                  catch (Exception $ex){

                        echo "Something went wrong " . $ex;
                    }

        }

1 个答案:

答案 0 :(得分:0)

您没有将变量绑定到结果集列

http://www.php.net/manual/en/mysqli-stmt.bind-result.php