doujionggan9570 2015-11-07 02:28
浏览 30
已采纳

使用oop php显示SELECT查询结果

I am a total noob with performing OOP, and am currently looking to refactor a procedural PHP project. Currently, I am having issues trying to simply display my results from a SELECT query--ironically doing an INSERT query made sense instead. This is my Class file:

<?php
 class MadLibs {                
    private $noun;
    private $verb;
    private $adverb;
    private $adjective;
    private $story;

    // Getters
    public function getNoun() {
        return $this->noun;
    }
    public function getVerb() {
        return $this->verb;
    }
    public function getAdverb() {
        return $this->adverb;
    }
    public function getAdjective() {
        return $this->adjective;
    }
    public function getStory() {
        return $this->story;
    }        
    // Setters
    public function setNoun($noun) {
        $this->noun = $noun;
    }
    public function setVerb($verb) {
        $this->verb = $verb;
    }
    public function setAdverb($adverb) {
        $this->adverb = $adverb;
    }
    public function setAdjective($adjective) {
        $this->adjective = $adjective;
    }
    public function setStory($story) {
        $this->story = $story;
    }
    // Figured this one out
    public function insertStoryToDatabase() {
        date_default_timezone_set('America/Chicago');
        $submit_date    = date('Y-m-d' . " " . 'H:i:s');            
        $dbc = mysqli_connect('localhost','root','','mad_libs')
            or die('Error establishing connection');                        
        $query  = "INSERT INTO storyTime (noun, verb, adjective, adverb, createdDate, fullStory) " .
                "VALUES ('$this->noun','$this->verb','$this->adjective','$this->adverb','$submit_date','$this->story')";            
        mysqli_query($dbc,$query)
                or die('Error querying database');                    
        mysqli_close($dbc);
    }
    // method I am having trouble setting up
    public function displayAllStories() {
        $dbc = mysqli_connect('localhost','root','','mad_libs')
            or die('Error establishing connection');                
        $result      = "SELECT fullStory, createdDate FROM storyTime ORDER BY createdDate DESC";            
        $display    = mysqli_query($dbc,$result)
                or die('Error gathering data!');            
        while ($row = mysqli_fetch_array($display)) {
            echo $row['story'] . '<br/>';
        }
        return $display;
    }      
  }    
?>

And this is what I am doing in my other php file:

<?php
    require_once('MadLibs.php');
    // Instantiating instance of MadLibs()
    $foo = new MadLibs();

    // Playing around to see ensure getters/setters were correct
    $foo->setNoun('Bob');
    $foo->setVerb('jumps');
    $foo->setAdverb('quietly');
    $foo->setAdjective('ugly');
    $foo->setStory("The " .  $foo->getNoun() . " always "  . $foo->getVerb() . " at the " 
. $foo->getAdjective() . " strangers " . $foo->getAdverb() . ".");    
    echo $foo->getNoun();
    echo '<br />';
    echo $foo->getVerb();
    echo '<br />';
    echo $foo->getAdverb();
    echo '<br />';
    echo $foo->getAdjective();
    echo '<br />';
    echo $foo->getStory();

    $foo->insertStoryToDatabase();

    $foo->displayAllStories();    
?>    

I'm sure it's something simple, but for the life of me I can't figure out what I'm doing wrong.

Any and all feedback is most appreciated!

  • 写回答

1条回答 默认 最新

  • doufuxing8691 2015-11-07 02:49
    关注

    You trying to display wrong variable in displayAllStories() function . 'echo $row['story']should be change to$row['fullStory'];` or change your query to

    SELECT fullStory as story, createdDate FROM storyTime ORDER BY createdDate DESC
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值