简体   繁体   English

mySQLi查询将结果放入数组和不同的DIV中

[英]mySQLi query to put results in an array and different DIVs

I want to query the content of multiple (6 in total, IDs 1 to 6) IDs from a mySQL database using mySQLi. 我想使用mySQLi从mySQL数据库查询多个(总共6个,ID 1至6)ID的内容。 After that I want to put the result ("introtext") in different DIVs. 之后,我想将结果(“ introtext”)放入不同的DIV中。 At the moment I use the following code (works perfect): 目前,我使用以下代码(完美运行):

<?php
$q = $db->query("SELECT introtext FROM content WHERE id=1");
$r = $q->fetch_object();
?>
<div id="content1" class="toggle"><?php echo "$r->introtext";?></div>

<?php
$q = $db->query("SELECT introtext FROM content WHERE id=2");
$r = $q->fetch_object();
?>
<div id="content2" class="toggle"><?php echo "$r->introtext";?></div>

<?php
$q = $db->query("SELECT introtext FROM content WHERE id=3");
$r = $q->fetch_object();
?>
<div id="content3" class="toggle"><?php echo "$r->introtext";?></div>

And so on... 等等...

How can I optimize this code to query mySQL only once (put the results in array) and assign the different results into the different DIVs? 如何优化此代码以仅查询mySQL一次(将结果放入数组中)并将不同的结果分配到不同的DIV中?

Cheers Shredder 干杯切碎机

Good question 好问题
Basically you will need 2 loops 基本上你需要两个循环
one to get your array with data: 一个让您的数组与数据:

<?php
$data = array();
$res = $db->query("SELECT id, introtext FROM content ORDER BY id LIMIT 6");
while($row = $res->fetch_assoc()) {
    $data[$row['id']] = $row['introtext'];
}
?>

You can use whatever SQL to filter out your queries. 您可以使用任何SQL来过滤查询。 ORDER BY id LIMIT 6 seems most convenient for the present case "get first 6 ids", as consecutiveness is never guaranteed. 对于当前情况“获取前6个ID”, ORDER BY id LIMIT 6似乎最方便,因为无法保证连续性。

And then another loop to display 然后显示另一个循环

<? foreach ($data as $id => $introtext): ?>
<div id="content<?=$id?>" class="toggle"><?=htmlspecialchars($introtext?)></div>
<? endforeach ?>

By the way, if use not raw Mysqli API but some helper library the PHP part will be reduced to one line: 顺便说一句,如果不使用原始的Mysqli API,而是使用一些帮助程序库,则PHP部分将减少为一行:

<?php
$data = $db->getIndCol("id","SELECT id, introtext FROM content ORDER BY id LIMIT 6");
?>

Why don't you use IN mysql operator? 为什么不使用IN mysql运算符?

SELECT introtext FROM content WHERE id IN (1,2,3,4,5,6);

Then fetch results in object/array . 然后在object / array中获取结果。

If the project is not huge i can suggest to give a try to RedBean ORM solution. 如果该项目规模不大,我建议尝试使用RedBean ORM解决方案。 To see how it works go here Is very simple and it requires nothing else than include the rb.php file in your project! 要查看其工作原理,请点击此处非常简单,除了在项目中包含rb.php文件之外,不需要任何其他操作!

By the way if you still want to use mysqli manually the solution is to make a new WHERE in your query with id >= 1 and id <= 6 and then use the php reference to extract the result to an array... mysqli quickstart guide 顺便说一句,如果您仍然想手动使用mysqli,则解决方案是在查询中使用id> = 1和id <= 6创建新的WHERE,然后使用php参考将结果提取到数组中。mysqli quickstart指南

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM