【发布时间】: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_query但mysql_fetch_array。你知道mysqli和mysql的区别吗? -
在您的表格中没有
table_id字段。 -
所以我什至不知道如何关闭它)))作为拼写错误或混合 api。
标签: php mysql templates smarty