我对这个php代码有真正的了解:
<?php
$sessionid = $_SESSION['id'];
echo $sessionid;
$sql = "SELECT * FROM users WHERE id = '$sessionid';";
$rsults = mysqli_query($conn, $sql);
$resultsCheck = mysqli_num_rows($rsults);
if ($resultsCheck > 0) {
while ($row = mysqli_fetch_assoc($rsults)){
$follow = $row['follow'];
$loc = $row['places'];
echo $follow;
echo $loc;
}
}
$sql = "SELECT * FROM posts WHERE username LIKE '$follow'";
$rsults = mysqli_query($conn, $sql);
$resultsCheck = mysqli_num_rows($rsults);
if ($resultsCheck > 0) {
while ($row = mysqli_fetch_assoc($rsults)){
echo '<div class="posts">';
echo '<img class="img"src='.$row['img'].' width="1500px">';
echo '</div>';
echo '<div class="contain">';
echo '<div class="over">';
echo '<div class="username2">';
echo '<img src="focus.png" width="25px" height="25px" style="padding-right: 10px;">'.'<a href="./Profile.php?data='.$row['username'].'">'.$row['username'].'</a>'.'<img src="loc.png" width="25px" height="25px" style="padding-right: 5px; padding-left: 10px;">'.'<a href="./Location.php?data='.$row['ext'].'">'.$row['ext'].'</a>';
echo '</div>';
echo '<div class="content">';
echo $row['content'];
echo '</div>';
echo '</div>';
}
}else {
echo "There are no results matching your search!";
}
?>
它将从第一个选择语句输出变量,但是第二个语句仅输出“没有符合您搜索条件的结果!”。我已经尝试在php我的管理员中使用实际的单词(不是变量)声明该语句,但是当它在站点上时,效果很好。我想这与变量有关,但实际上我不知道,因此任何帮助将不胜感激!非常感谢 (而且我知道变量可以进行sql注入,但是此站点仅是概念证明:)
答案 0 :(得分:0)
对于第二条SQL语句,您需要将%
与LIKE运算符结合使用。
%
-百分号代表零个,一个或多个字符
要更深入地了解,您可以点击以下链接:SQL LIKE Operator
更改您的第二条SQL语句,如下所示。我认为它会按您的预期工作。
$sql = "SELECT * FROM posts WHERE username LIKE '%$follow%'";
答案 1 :(得分:0)
尝试一下,我做了一些调整,所以它可以工作
<?php
$sessionid = $_SESSION['id'];
echo $sessionid;
$sql = "SELECT * FROM users WHERE id = '".$sessionid."'";
$rsults = mysqli_query($conn, $sql);
$resultsCheck = mysqli_num_rows($rsults);
if ($resultsCheck > 0) {
while ($row = mysqli_fetch_array($rsults)){
$follow = $row['follow'];
$loc = $row['places'];
echo $follow;
echo $loc;
}
}
$sql = "SELECT * FROM posts WHERE username LIKE '".$follow."'";
$rsults = mysqli_query($conn, $sql);
$resultsCheck = mysqli_num_rows($rsults);
if ($resultsCheck > 0) {
while ($row = mysqli_fetch_array($rsults)){
echo '<div class="posts">';
echo '<img class="img"src='.$row['img'].' width="1500px">';
echo '</div>';
echo '<div class="contain">';
echo '<div class="over">';
echo '<div class="username2">';
echo '<img src="focus.png" width="25px" height="25px" style="padding-right: 10px;">'.'<a href="./Profile.php?data='.$row['username'].'">'.$row['username'].'</a>'.'<img src="loc.png" width="25px" height="25px" style="padding-right: 5px; padding-left: 10px;">'.'<a href="./Location.php?data='.$row['ext'].'">'.$row['ext'].'</a>';
echo '</div>';
echo '<div class="content">';
echo $row['content'];
echo '</div>';
echo '</div>';
}
}else {
echo "There are no results matching your search!";
}
?>