简体   繁体   English

使用mysqli获取数据

[英]using mysqli to fetch data

i've been trying my hand at mysqli. 我一直在尝试mysqli。

using procedural mysql, the code displays the results fine. 使用过程mysql,代码可以很好地显示结果。 however, after making the switch to OOP mysqli, i tried converting the code to mysqli, and it says there are no quotes. 但是,在切换到OOP mysqli之后,我尝试将代码转换为mysqli,它说没有引号。 is there anything wrong with the code? 代码有什么问题吗?

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

        }

You didn't bind variables to resultset columns 您没有将变量绑定到结果集列

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM