【问题标题】:php db query to display certain user data (Smarty included)php db 查询以显示某些用户数据(包括 Smarty)
【发布时间】:2017-06-06 11:45:12
【问题描述】:

我已经设法创建了一个登录表单,它从数据库获取用户数据并显示用户数据,但我现在想显示与该用户相关的 table_name。

问题是我将不得不使用JOIN,因为它需要从 2 个表中查询数据。

我的问题

我不断收到错误,我不确定我在php 文件中的查询是否有错误的语法,以及我的方法是否只是错误的方法。

连接

请记住我的数据库连接 ($db) 可以完美运行,因为用户已经可以使用用户名和密码登录

错误

注意:未定义变量:第 42 行 C:\xampp\htdocs\score4score\table.php 中的行

数据库:表格

table: sc_tables
+------+----------+-------------+
| t_id | user_id  | table_name  |
+------+----------+-------------+
|  1   |     1    | bobs        |
+------+----------+-------------+

table: sc_scores
+------+----------+-------------+-------+
| s_id | user_id  |    t_id     | score |
+------+----------+-------------+-------+
|  1   |     1    |      1      |  50   |
+------+----------+-------------+-------+

PHP

<?php
   include("config.php");
   session_start();

   $userId = (isset($_GET["user_id"]) && is_numeric($_GET["user_id"]) && (int)$_GET["user_id"] > 0) ? (int)$_GET["user_id"] : 0;

   $query = "SELECT `a`.`table_name` FROM `sc_tables` AS `a` JOIN `sc_scores` AS `b` ON `a`.`t_id` = `b`.`t_id`";

   $result = mysqli_query($db, $query);

     $rows = array();

     while ($row = mysql_fetch_array($result)) {
     $rows[] = $row;
     }

   $smarty->assign('rows', $rows); 
   include("header.php");
   $smarty->display('records.tpl');

 ?>

TPL (records.tpl)

<div>
 <div>
    The current rankings table: 
    <table>

        {foreach from=$rows item="row"}
        <tr>
            <td>{$row.table_name}</td>
        </tr>
        {/foreach}

    </table>
 </div>
</div>

如果有人能指出我正确的方向,将不胜感激。谢谢

【问题讨论】:

  • 您的数据库查询导致错误,这就是 $result 为 false 的原因。所以请去研究你需要做什么,首先找出这个错误是什么。
  • 谢谢,你认为这可能是语法吗?或者至少是正确的?
  • mysqli_querymysql_fetch_array。你知道mysqlimysql的区别吗?
  • 在您的表格中没有table_id 字段。
  • 所以我什至不知道如何关闭它)))作为拼写错误或混合 api。

标签: php mysql templates smarty


【解决方案1】:

表中没有table_id 字段。所以,你应该有:

$query = "SELECT `a`.`table_name` FROM `sc_tables` AS `a` JOIN `sc_scores` AS `b` ON `a`.`t_id` = `b`.`t_id`";

错误表明值 FALSE 作为参数传递给 mysql_fetch_array($result)。例如。您的 mysqli_query() 返回 FALSE,而不是应有的 mysqli_result 对象。

如果修改后问题仍然存在,没有看到任何错误,那么你应该同时使用mysqli_errno()mysqli_error()mysqli_error_list()

祝你好运!

【讨论】:

  • 谢谢,是的,对不起,我在这方面有点快,我实际上使用 table_id 这只是一个示例数据库表,我编辑我的帖子。我确实错过了 mysqli_query() 中的 i 现在我的错误是通知:未定义的变量:行 i,所以现在我要对此进行更多调查.. 谢谢 man
  • @Ylama 没问题。我在回答后阅读了 cmets,所以我是最新的:-)
  • @Ylama 让我猜猜:你从模板中的item="$row" 中删除了 (") :-) 不客气。祝你好运。
  • 不行,没有注入 mysqli_query() 哈哈,菜鸟的错误,但一切都很好,因为我完全是菜鸟
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-18
  • 1970-01-01
  • 1970-01-01
  • 2013-01-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多