gpt4 book ai didi

php - mysqli_fetch_assoc while 循环不工作

转载 作者:行者123 更新时间:2023-11-29 11:08:58 24 4
gpt4 key购买 nike

* if(!empty) 检索数组中的第一个条目,不应使用 *

此脚本在数据库中搜索与用户在表单中提交的 URL 相匹配的 URL。目前,echo 语句打印的 $query 在 mysql 中工作正常并返回一项。该脚本执行并打印表头,但不进入 while($row =.. ) 循环。

任何想法或建议将不胜感激。我以前用过这个方法,没有遇到任何问题,所以现在我有点困惑。

//1. Query DB for results

$query = "SELECT * FROM projects WHERE projectsurl='".$url."';";
echo "<br>".$query;
$projects = mysqli_query($conn, $query);

// 2. Print Project Results

if(!empty(mysqli_fetch_assoc($projects))){
//Print Project Results
echo "Is this your project?<br>";
echo "<table style=width:'100%'>";
while ($row = mysqli_fetch_assoc($projects)){

echo $tabler . $thh . "Title: <br>" .$row['title'] . $thsp . "No. Rewards: <br>" . $row['rewards'] . $thf . $xtabler;
echo $tabler . $thh . "ID: " .$row['id'] . $thf . $xtabler;
// Echo two rows for the URL Strings because they are longer.
echo $tabler . $thh . "<a href='" . $row['projectsurl'] . "'>Projects</a>" . $thsp . "<a href='" . $row['rewardsurl'] . "'> Rewards " . $thf . $xtabler;
echo "<form id='confirmation' action='../index.php' method='POST'>
<input type='hidden' value='2' name = 'stage'>
<input type='hidden' value='".$row['id']."' name='id'>
<input type='hidden' value='".$row['title']."' name='title'>
<input type='submit' value='Confirm'></form>";
}

echo "</table>";


}else{
//trigger ruby script to search for lost file
echo "Project Not Found. <br>";




}

哦,随机表的内容在其他地方定义为

$tabler = "<tr>";
$xtabler = "</tr>";

$thh = "<th><b>";
$thsp = "</th><th>";
$thf = "</b></th>";

$csp = "</td><td>";
$ch = "<td>";
$cf = "</td>";

最佳答案

如果只有一行,则 if(!empty(mysqli_fetch_assoc($projects))){ 将获取它,并且不会进入 while 循环。相反,要检查行是否存在,请使用 mysqli_num_rows($projects)。

另外,提醒,如果您还没有在 MySQL 查询中使用用户提交的数据,请先对其进行转义。这可以通过以下方式完成:$query = "SELECT * FROMprojectsWHEREprojectsurl='".mysqli_real_escape_string($conn, $url)."';";

编辑:或者,在执行 MySQL 语句之前准备它们。这是首选方法,尽管这两种方法都可以保护您免受注入(inject)。

关于php - mysqli_fetch_assoc while 循环不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40952455/

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