[英]Store data from MySQL into an array and access it later
Eh.. I am sorry for asking such a basic question but I couldn't find a good answer neither on google or stackoverflow... here is what I want to do. 恩。很抱歉问这样一个基本问题,但是我在Google或stackoverflow上都找不到很好的答案...这就是我想要做的。
I save announcements in MySQL and then I show them to the users using 我将公告保存在MySQL中,然后使用显示给用户
<?php while($announcerow = mysqli_fetch_assoc($announceResult)): ?>
then there is a button for admins to edit them, a modal opens and the admin is able to select which announcement he wants to edit, well, I try to show the announcements same way there too but if I do that the announcements wont show anymore, so I can use mysqli_fetch_assoc only once on a query as I see... 然后有一个按钮供管理员编辑,一个模式会打开,管理员可以选择他要编辑的公告,好吧,我也尝试以相同的方式显示公告,但是如果我这样做,公告将不再显示,因此我只能在查询中使用mysqli_fetch_assoc一次,如我所见...
I thought to save them in an array and use them later, but then I do not know how to access the array later. 我曾想将它们保存在数组中,以后再使用,但是后来我不知道如何稍后访问数组。
for example, I save them like this: 例如,我这样保存它们:
while($row = mysql_fetch_assoc($query)){
// add each row returned into an array
$array[] = $row;
}
but how do I show the announcements later for the users? 但是以后如何为用户显示公告? also, my announcements have an ID, a title, an URL and a description, this is how I show them right now.
另外,我的公告有ID,标题,URL和说明,这就是我现在显示的方式。
<?php while($announcerow = mysqli_fetch_assoc($announceResult)): ?>
<a data-toggle="tooltip" title="<?php echo $announcerow['Description']; ?>" target="_blank" href="<?php echo $announcerow['URL']; ?>"><p class="text-light-blue"><span class="fa fa-external-link"></span> <?php echo $announcerow['Title']; ?><span class="text-muted"> - by <?php echo $announcerow['Author']; ?> (<?php echo $announcerow['Date']; ?>)</span></p></a>
<?php endwhile; ?>
how will I show them from the array? 我如何从数组中显示它们?
Sorry for this noobish question... couldn't find an answer. 很抱歉这个愚蠢的问题...找不到答案。
I even tried mysql_data_seek(); 我什至尝试了mysql_data_seek(); so I can use fetch more times but I got this error:
所以我可以使用fetch更多次,但是出现了这个错误:
Warning: mysql_data_seek(): supplied argument is not a valid MySQL result resource in
I've tried this 我已经试过了
while($row = mysql_fetch_assoc($announceResult)){
$array[] = $row;
}
and 和
<?php foreach($array as $datum): ?>
<a data-toggle="tooltip" title="<?php echo $datum['Description']; ?>" target="_blank" href="<?php echo $datum['URL']; ?>">
<p class="text-light-blue">
<span class="fa fa-external-link"></span>
<?php echo $datum['Title']; ?>
<span class="text-muted"> - by <?php echo $datum['Author']; ?> (<?php echo $datum['Date']; ?>)
</span>
</p>
</a>
<?php endforeach; ?>
but I get the following errors 但我收到以下错误
Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /home/a8438725/public_html/index.php on line 56
Notice: Undefined variable: array in /home/a8438725/public_html/index.php on line 777
Warning: Invalid argument supplied for foreach() in /home/a8438725/public_html/index.php on line 777
Here is the whole code of the sql and array 这是sql和array的整个代码
$announceSql = "SELECT `ID`, `Author`, `Title`, `Date`, `URL`, `Description` FROM `Announcements` ORDER BY ID DESC";
$announceResult = mysqli_query($db, $announceSql);
$announcerow_cnt = mysqli_num_rows($announceResult);
while($row = mysql_fetch_assoc($announceResult)){
$array[] = $row;
}
I've also added this 我也添加了这个
if (!$announceResult) {
printf("Error: %s\n", mysqli_error($db));
exit();
}
and there is no mysql error, or atleast no mysql error appeard 并且没有mysql错误,或者至少没有mysql错误出现
SOLVED! 解决了! I was using mysql instead of mysqli_fetch_assoc
我正在使用mysql而不是mysqli_fetch_assoc
Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /home/a8438725/public_html/index.php on line 56
警告:mysql_fetch_assoc():提供的参数不是第56行/home/a8438725/public_html/index.php中的有效MySQL结果资源
That means the line you haven't shown us containing something like 这意味着您未向我们显示的行包含类似
$announceResult=mysqli_query($db_conn, $sql);
failed or $announceResult is in a different scope, or $announceResult has been changed since it was populated. failed或$ announceResult在不同的范围内,或者$ announceResult自填充以来已更改。 The first of these would likely have triggered a warning too.
这些中的第一个可能也会触发警告。 So the most likely cause is that you've got a scope issue or bug somewhere in the 750+ lines of code you've not shown us here.
因此,最可能的原因是您在750多个代码行中的某个地方遇到了范围问题或错误,而您在此处未向我们展示。
(hint: if you run into a problem like this and can't fix it in situ, it is advisable to try writing as small a program as necessary to reproduce the fault and use that to undertand the problem / illustrate your issue on Stack Overflow). (提示:如果遇到这样的问题并且无法就地解决,建议尝试编写必要的小程序以重现故障,并利用它来理解问题/在Stack Overflow上说明问题)。
Notice: Undefined variable: array in /home/a8438725/public_html/index.php on line 777
注意:未定义的变量:第777行的/home/a8438725/public_html/index.php中的数组
Warning: Invalid argument supplied for foreach() in /home/a8438725/public_html/index.php on line 777
警告:第777行的/home/a8438725/public_html/index.php中为foreach()提供的参数无效
Since the loop populating $array() is driven by mysqli_fetch_assoc(), $array() was never initialized and populated. 由于填充$ array()的循环是由mysqli_fetch_assoc()驱动的,因此从未初始化和填充$ array()。
You can use like this 你可以这样使用
<?php while($announcerow = mysqli_fetch_assoc($announceResult)){ ?>
$array[] = $announcerow;
<?php }?>
<?php foreach($array as $datum) {?>
<a data-toggle="tooltip" title="<?php echo $datum['Description']; ?>" target="_blank" href="<?php echo $datum['URL']; ?>">
<p class="text-light-blue">
<span class="fa fa-external-link"></span>
<?php echo $datum['Title']; ?>
<span class="text-muted"> - by <?php echo $datum['Author']; ?> (<?php echo $datum['Date']; ?>)
</span>
</p>
</a>
<?php } ?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.