我一直在查看其他帖子,例如this和this,但对我的具体情况没有任何作用。
我有一张这样的桌子:
| Name | Reference | Etc... |
|------------|-----------------|--------------|
| John Doe | | Blah blah |
| Jane Doe | John Doe | Blah blah |
| Mike Small | Jane Doe | Blah blah |
| Steve Ex | John Doe | Blah blah |
| Mary White | Mike Small | Blah blah |
我希望能够找到哪些名称也是另一个名称的引用并将它们转换为链接,以便用户可以单击它们并获取引用的名称列表。
例如,如果用户点击 John Doe,他/她将获得如下表格:
| Name | Reference | Etc... |
|------------|-----------------|--------------|
| Jane Doe | John Doe | Blah blah |
| Steve Ex | John Doe | Blah blah |
目前我被困在这里:
function find_items_by_ref($ref) {
db_connect();
$query = sprintf("SELECT * FROM items WHERE reference LIKE '%s'", mysql_real_escape_string($ref));
$result = mysql_query($query);
$row = mysql_fetch_array($result);
return $row;
}
我已经尝试过LIKE
等等CONTAINS
等等like '%' || TEXT || '%'
,但没有任何效果。请问有什么想法吗?
我在下面给出的答案中尝试了这两种可能性。第一个我遇到了一些麻烦。当我想要echo
从 SQL Select 获得的数组元素时,我收到警告:非法字符串偏移 'X' ... 错误。如果我var_dump
在其中一个检索到的数组上运行,这就是我得到的。我不太清楚出了什么问题。对我来说似乎是一个工作数组:
array (size=16)
0 => string '37' (length=2)
'id' => string '37' (length=2)
1 => string 'Steve Ex' (length=8)
'name' => string 'Steve Ex' (length=8)
2 => string 'John Doe' (length=8)
'reference' => string 'John Doe' (length=8)
3 => string 'Blah blah' (length=9)
'etc' => string 'Blah blah' (length=9)
好的。解决了。这个问题太愚蠢了……猜猜谁觉得自己像个混蛋。无论如何,如果 SQL 行作为一行,我试图传递一个数字。当用户想要通过其 ID 获取单行信息时,我错误地复制了我的 Select 函数。问题一直就在那里,我就像一头躲在树后面的大象一样想念它。无论如何,它是......对不起。工作代码是:
function find_items_by_ref($ref) {
db_connect();
$query = sprintf("SELECT * FROM items WHERE INSTR(reference,'".mysql_real_escape_string($ref)."')>'0' ORDER BY name ASC");
$result = mysql_query($query);
$result = db_result_to_array($result);
return $result;
}
感谢@dimaninc 的帮助!