从单元格获取数据

发布于 2024-11-29 05:02:34 字数 409 浏览 2 评论 0原文

我有一个名为“设置”的数据库表,在该表中我有 2 列。示例图片:

在此处输入图像描述

如您所见,第一列称为“setting_name” ”,第二个称为“setting_value”。

我该怎么办,所以每当我写 example: 时,

echo $setting['ad_cost_micro'];

它都会打印出:

0.00200

顺便说​​一句。是否可以不使用 while 语句?

I have a database table called "settings", and in that table I've got 2 columns. Example picture:

enter image description here

As you can see, the first columns is called "setting_name" and the second is called "setting_value".

How can I do, so whenever I write example:

echo $setting['ad_cost_micro'];

It will print out:

0.00200

Btw. is it possible to do without a while statement?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

半边脸i 2024-12-06 05:02:34

我倾向于使用 PDO 和 fetchAll()。

$stmt = $db->prepare('SELECT `setting_name`, `setting_value` FROM `settings`');
$stmt->execute();
$setting = $stmt->fetchAll();
echo $settings['ad_cost_micro']; //Should output 0.00200.

注意:无论您使用什么,while 循环是不可避免的,因为您必须循环遍历结果集 (fetchAll()),而且要知道每个循环是一个 while 循环(甚至是 for 循环!)。

I tend to use PDO, and fetchAll().

$stmt = $db->prepare('SELECT `setting_name`, `setting_value` FROM `settings`');
$stmt->execute();
$setting = $stmt->fetchAll();
echo $settings['ad_cost_micro']; //Should output 0.00200.

Note: Regardless of what you use, a while loop is unavoidable because you HAVE to loop through the result set (fetchAll()) does that too, also, know that every loop is a while loop (even for loops!).

可遇━不可求 2024-12-06 05:02:34

如果 mysqli 可用...

$mysqli = new mysqli(hostname, username, password, database);
$result = $mysqli->query('SELECT setting_name,setting_value FROM settings');

if ($result->num_rows > 0)
{
    $rows = $result->fetch_array();
    $setting = array();
    for($i = 0; $i <= $result->num_rows(); $i++)
    {
        $setting[$rows[$i]['setting_name']] = $row[$i]['setting_value'];
    }

    unset($rows);
    $result->free();
}
else
{
    // error handling
}

$mysqli->close();

编辑:删除 while 代替 for

if mysqli is available...

$mysqli = new mysqli(hostname, username, password, database);
$result = $mysqli->query('SELECT setting_name,setting_value FROM settings');

if ($result->num_rows > 0)
{
    $rows = $result->fetch_array();
    $setting = array();
    for($i = 0; $i <= $result->num_rows(); $i++)
    {
        $setting[$rows[$i]['setting_name']] = $row[$i]['setting_value'];
    }

    unset($rows);
    $result->free();
}
else
{
    // error handling
}

$mysqli->close();

EDIT: removed while in place of for

云雾 2024-12-06 05:02:34

看不到截图,但也许...

    $query = mysql_query("SELECT * FROM settings");
    while ($row = mysql_fetch_array($query, MYSQL_ASSOC)) {
        $setting[$row['setting_name']] = $row['setting_value'];
    }

Can't see the screenshot but maybe...

    $query = mysql_query("SELECT * FROM settings");
    while ($row = mysql_fetch_array($query, MYSQL_ASSOC)) {
        $setting[$row['setting_name']] = $row['setting_value'];
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文