【问题标题】:PHP + Smarty + MySQLPHP + Smarty + MySQL
【发布时间】:2013-07-27 12:08:52
【问题描述】:

请帮帮我!如何将数据从 table 传输到 smarty?

功能:

public function getBanLog() {
    global $mysqli;
    $result = $query = $mysqli->query("SELECT * FROM `bans`") or die($mysqli->error);
    $rows = array();
    while($row = $result->fetch_array(MYSQLI_ASSOC)) {
        $rows[] = $row;
    }
}

index.php:

$user = new UserInfo();
$smarty = new Smarty();

$smarty->assign("userInfo", $user);
$smarty->assign('ban', $user->getBanLog());
$smarty->display('template/ban.tpl');

ban.tpl:

{foreach from=$ban item=row}
    <td>{$row.id}</td>
    <td>{$row.banned}</td>
    <td>{$row.admin}</td>
    <td>{$row.reason}</td>
{/foreach}

【问题讨论】:

  • 代码看起来不错有什么问题?
  • 这里唯一值得怀疑的是您的查询中缺少 WHERE 子句,这似乎会返回所有用户的所有禁令,而不仅仅是 UserInfo() 所暗示的用户。
  • @dianuj getBanLog() 什么也不返回,$result = $query = $mysqli-&gt;query

标签: php mysql smarty


【解决方案1】:

你的getBanLog()函数什么都不返回,需要添加return语句。 $result = $query = $mysqli-&gt;.. 也不正确。

试试这个

public function getBanLog() {
    global $mysqli;
    $result = $mysqli->query("SELECT * FROM `bans`") or die($mysqli->error);
    $rows = array();
    while($row = $result->fetch_array(MYSQLI_ASSOC)) {
        $rows[] = $row;
    }
    return $rows;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多