gpt4 book ai didi

php - MySQL查询: load the information into the form fields in the CMS

转载 作者:行者123 更新时间:2023-11-29 09:14:49 26 4
gpt4 key购买 nike

我遇到了一些问题,我目前正在开发一个自定义 CMS,它允许用户更改网站标题、描述、关键字和页脚,以及各种其他设置,这些设置都保存在一个名为 site_settings 的 mysql 表有两列

设置和内容,设置列保存设置名称,例如标题,内容保存诸如“欢迎来到我的网站”之类的信息,这些信息通过各种查询加载到站点中,效果很好,麻烦我我想将信息加载到 CMS 中的表单字段中,我希望通过以下查询来完成此操作

$query  = mysql_query("SELECT `setting`, `content` FROM `site_settings`");

然后使用以下信息创建一个数组:

$content = mysql_fetch_array($query);

但获取信息的唯一方法是创建一个 while 语句,循环遍历每一行,直到到达末尾,但我想要做的是使用表中的设置列作为主要标识符检索内容

例如,目前使用 while 数组,我的数组如下所示:

Array ( [0] => title [1] => title [2] => title [3] => title )
Array ( [0] => keywords [1] => keywords [2] => keywords [3] => keywords )
Array ( [0] => description [1] => description [2] => description [3] => description )
Array ( [0] => footnote [1] => footnote [2] => footnote [3] => footnote )
Array ( [0] => notice_state [1] => notice_state [2] => 1 [3] => 1 )
Array ( [0] => maintenance_state [1] => maintenance_state [2] => 1 [3] => 1 )
Array ( [0] => notice_message [1] => notice_message [2] => notice [3] => notice )
Array ( [0] => maintenance_message [1] => maintenance_message [2] => maintenance [3] => maintenance )
Array ( [0] => about_us [1] => about_us [2] => about us [3] => about us )
Array ( [0] => article_shorten_length [1] => article_shorten_length [2] => 80 [3] => 80 )

所以 id 必须循环遍历它们才能检索所有信息,并使用以下语句:

while($content = mysql_fetch_array($query)) {
echo $content['content'];
}

但我想做的是将它们全部放入一个数组中,并使用 1 个或几个 mysql 语句,如下所示

Array (

title -> 'welcome to my website',
description -> 'this is my website',
)

等等...

并且只需从我的数组中调用设置列来回显信息,而不使用 while 语句,也无需为每一行进行单独的 mysql 查询,例如:

$content['title'];
$content['description'];

有办法做到这一点吗?

最佳答案

使用 mysql_fetch_row 的循环:

while ($row = mysql_fetch_row($query))
{
$content[$row[0]] = $row[1];
}

这可以避免将整个结果集读入数组,然后将其丢弃。因此,它可能会更有效。

关于php - MySQL查询: load the information into the form fields in the CMS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4478597/

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