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 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥50 我撰写的python爬虫爬不了 要爬的网址有反爬机制
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据