doushui3216 2014-10-17 22:10
浏览 10

获取信息的php函数[关闭]

I'm trying to make it so it shows the latest announcements in a link with the title. I've tried doing it and now I'm just trying to get it to even return the variable and it won't...

Functions.php

<?php
    require("common.php");
    function getAnnouncements() {
        $query = "SELECT * FROM announcements";

        try {
            global $db;
            // Execute the query against the database
            $stmt = $db->prepare($query); 
            $result = $stmt->execute();
            echo $result['title'];
        }
        catch(PDOException $ex) { 
            // Note: On a production website, you should not output $ex->getMessage(). 
            // It may provide an attacker with helpful information about your code.  
            die("Failed to run query: " . $ex->getMessage()); 
        } 

    }

?>

index.php

    <div class="announcement">
        <div class="container">
            Latest Announcements: <?php getAnnouncements(); echo "<a href=''></a>" ?>
        </div>
    </div>

Yes, I am including the file at the top and there's no spelling errors in it.

  • 写回答

1条回答 默认 最新

  • dpmp9359 2014-10-17 22:15
    关注

    First, make sure that $db is initialized with:

     $db = new PDO("mysql:host=[host here];dbname=[dbname]e;charset=utf8",[username],[password]);
    

    You're going to want to fetch an associative array and loop through it like this:

    $result = $result->fetchAll(PDO::FETCH_ASSOC);
    for($result as $a_result)
        echo '<a href="'.$a_result['link'].'">'.$a_result['title'].'</a>';
    

    This is assuming there is a link column on your database

    评论

报告相同问题?

悬赏问题

  • ¥15 Macbookpro 连接热点正常上网,连接不了Wi-Fi。
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析