gpt4 book ai didi

php - MySQLi 从有限制的两个表中选择

转载 作者:行者123 更新时间:2023-11-29 00:05:35 25 4
gpt4 key购买 nike

我一直在尝试合并mysql 数据库中的两个表,这两个表是status 和status_reply 都有相同的列号和名称,即 id, account_name, author, postdate, data 请提供帮助,我们将不胜感激。

            $limit = "LIMIT 0, 10";
$query = mysqli_query($db_conx, "(SELECT * `status` as type from status WHERE data LIKE '%".$tag."%' ORDER BY postdate DESC $limit)
UNION (SELECT * `status_reply` as type from status_reply WHERE data LIKE '%".$tag."%' ORDER BY postdate DESC $limit)");

//$query = mysqli_query($db_conx, "SELECT * FROM status WHERE data LIKE '%$tag%' ORDER BY postdate DESC $limit");
$statusnumrows = mysqli_num_rows($query);
while ($row = mysqli_fetch_array($query, MYSQLI_ASSOC)) {
$statusid = $row["id"];
$account_name = $row["account_name"];
$author = $row["author"];
$postdate = $row["postdate"];
$data = $row["data"];
$data = nl2br($data);
$data = str_replace("&","&",$data);
$data = stripslashes($data);
$statuslist .= '<div id="status_'.$statusid.'" class="status_boxes"><div><b>Ivotised by <a href="home.php?u='.$author.'">'.$author.'</a> '.$postdate.':</b>
<article>'.$data.'</article>
</div></div>';
}

最佳答案

  1. 对字段名称使用反引号 ` 而不是直引号 '
  2. 不要忘记引用 $tag 以防止 SQL 注入(inject):mysqli_real_escape_string
  3. 请记住,您要逐字搜索 LIKE 通配符“%”、“_”以及反斜杠。您也需要转义它们,使用:$tag = addcslashes($tag, '\%_');

$limit = "LIMIT 0, 10";
$query = mysqli_query($db_conx, "
(SELECT `status` as type from status WHERE data LIKE '%".$tag."%'
ORDER BY postdate DESC $limit)
UNION
(SELECT `status_reply` as type from status_reply WHERE data LIKE '%".$tag."%'
ORDER BY postdate DESC $limit)");

关于php - MySQLi 从有限制的两个表中选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27634633/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com