我正在尝试编写一些PHP代码,这些代码将查询数据库表的最后10行(由三个字段组成),并搜索字符串/子字符串"word“的任何实例并对它们进行计数--包括在单个单元格中进行多次计算。以下是我到目前为止所拥有的:
$query = "SELECT * from my_table WHERE column1 LIKE '%word%'
UNION ALL SELECT * from my_table WHERE column2 LIKE '%word%'";
$result = mysqli_query($mysqli, $query);
$i = 0;
while($row = mysqli_fetch_array($result))
{
$i++;
}
echo "<i>This table contains [" . $i . "] instances of the string \"word\".</i>";
mysqli_free_result($result);
mysqli_close($mysqli);我遇到的问题是:
我花了好几个小时在这件事上--我很想听听你的建议。非常感谢!
/** UDPATE **/
这就是我最后的下场。本质上,我将查询转换为数组,然后转换为字符串。在那里,我搜索了所有“单词”的实例。谢谢大家的帮助。
$query = "SELECT * FROM my_table ORDER BY PID DESC LIMIT 10";
$result = mysqli_query($mysqli, $query);
$j = 0;
$i = 'word';
while ($row = mysqli_fetch_array($result))
{
$intoarray = implode(",", $row);
$j += substr_count($intoarray, $i);
}
echo "<i>This table contains [" . ($j / 2) . "] instances of the string \"word\".</i>";发布于 2014-02-14 19:13:52
尝试对表和列的所有名称使用回勾符号。你可以在你的美国键盘上找到`,就在你的<TAB>键上方。例如:
原来的答案:
"SELECT * from `my_table` WHERE `column1` LIKE '%word%'"根据对此的评论编辑:
$query = "SELECT * from `my_table` WHERE `column1` LIKE '%word%'
UNION ALL SELECT * from `my_table` WHERE `column2` LIKE '%word%'";此查询在我的服务器上运行良好。
发布于 2014-02-14 19:30:36
我为我的表创建了下面的查询,它运行良好
(select * from eas_appliciant
where appliciant_id = '6'
order by appliciant_id limit 0,1)
union all
(select * from eas_appliciant
where appliciant_id = '7'
order by appliciant_id limit 0,1)您还可以创建以下内容
( SELECT column1 as selected_column from my_table
WHERE column1 LIKE '%word%'
ORDER BY PID DESC LIMIT 0,10 )
UNION ALL
( SELECT colum2 as selected_column from my_table
WHERE column2 LIKE '%word%'
ORDER BY PID DESC LIMIT 0,10)我不知道它为什么不适合你
https://stackoverflow.com/questions/21787584
复制相似问题