我已经设法创建了一个登录表单,该登录表单从数据库中获取用户数据并显示用户数据,但是我现在想显示该用户与之相关的table_name。
问题是我将不得不使用JOIN
,因为它需要从2个表中查询数据。
我的问题
我一直遇到错误,我不确定我的php
文件中的查询是否错误,并且我的方法是否只是错误的方法。
连接
请记住我的数据库连接($db
(,因为用户已经可以使用用户名和密码登录
错误
注意:未定义的变量:c: xampp htdocs score4score table.php in Line 42
数据库:表
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>
如果有人只能将我指向正确的方向,这将不胜感激。谢谢
表中没有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()
。
祝你好运!