简体   繁体   English

将来自MySQL的数据存储到数组中,以后再访问

[英]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.

相关问题 如何将数组存储在文件中,以便以后使用PHP作为数组访问? - How do I store an array in a file to access as an array later with PHP? 在Mysql中存储一个Facebook Access令牌,以便以后发布到墙上 - Store a Facebook Access token in Mysql to post to wall later PHP-如何在数组中存储数组键并在以后使用它访问值 - PHP - How to store an arrays keys in an array and use it to later access the value 从mysql服务器访问和存储大量数据 - Access and store large amount of data from mysql server 如何将数组中的数据存储到mySQL数据库中? - How can I store data from an array into the mySQL database? 如何在php中从mysql数据库将数据存储为关联数组 - how to store data as an associative array from mysql database in php 如何使用MYSQL从数据库获取数据并将其存储在javascript数组中 - how to get a data from a DB using MYSQL and store it in javascript Array 如何将mySQL数据库中的数据存储到数组中? - How can I store data from mySQL database into an array? 如何处理来自数据库中存储的文本编辑器的数据并稍后显示 - how to handle data from text editors store in database and later display 如何通过匹配(字符串到数组和数组到数组)从 mysql 存储和检索数组数据 我在从 MySQL 获取数据时遇到问题 - how to store and retrive array data from mysql by match (string to array AND array to array) i have problem in fetching data from MySQL
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM