如何阻止 MySQL 复制返回数组中的每一列条目?

发布于 2024-09-19 04:12:06 字数 495 浏览 3 评论 0原文

我的 MySQL 查询返回包含重复条目的数组:编号键和标记键,其中包含相同的数据。这可能是标准的,但似乎很浪费,并且如果我打印值可能会导致问题。我的意思是,显然这不是一个大问题。但我只是好奇我是否能阻止它。看来没有必要。例如:

Array(
    [0] => "Ted",
    [first_name] => "Ted",
    [1] => "Schmidlap",
    [last_name] => "Schmidlap"
)

等等。

我对很多方面都很陌生,所以这可能是一个简单的问题,但谷歌搜索似乎没有给我任何答案。有谁知道发生这种情况的原因吗?我现在使用 PHP 的 PDO,但之前我直接通过 MySQL 函数进行操作,并且发生了同样的事情,所以我认为它是 MySQL 交互的副产品。

我可以迭代并取消设置数字,因为我不需要它们,但它们现在并不真正妨碍,所以这只是一个额外的步骤。不过,有没有一种方法可以让它们一开始就不被获取呢?

My MySQL queries are returning arrays with duplicate entries: numbered keys and labeled keys with the same data inside. This may be standard, but it seems like a waste, and something that could cause problems if I'm printing values. I mean, not a huge problem, obviously. But I'm just curious if I can stop that. It seems unnecessary. For example:

Array(
    [0] => "Ted",
    [first_name] => "Ted",
    [1] => "Schmidlap",
    [last_name] => "Schmidlap"
)

And so on.

I'm pretty new to a lot of this, so this may be a simple question, but Googling doesn't seem to have any answers for me. Anyone know the reason this happens? I'm using PHP's PDO now, but I was doing it straight through the MySQL functions before and the same thing was happening, so I assume it's a byproduct of MySQL interaction.

I can iterate through and unset the numeric ones, because I don't need them, but they're not really in the way right now, so that's just an extra step. Still, is there a way to simply not have them fetched in the first place?

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

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

发布评论

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

评论(4

酷遇一生 2024-09-26 04:12:06

据推测,这种情况是在您使用 mysql_fetch_array (或类似的)之后发生的。

您需要添加一个标志来指定您想要返回的数组类型,或者 PHP 假定这两种类型。

即 mysql_fetch_array($result_set, MYSQL_ASSOC|MYSQL_NUM|MYSQL_BOTH)

Presumably this is happening after you use mysql_fetch_array (or similar).

You need to add in a flag to specify what array type you want returned or PHP assumes both.

i.e. mysql_fetch_array($result_set, MYSQL_ASSOC|MYSQL_NUM|MYSQL_BOTH)

徒留西风 2024-09-26 04:12:06

这取决于您使用的功能。
有些函数返回两种类型,其他函数仅返回其中一种类型。

如果您使用 PDOStatement->fetch,请注意 它需要可选的$fetch_style参数

That depends on the function you are using.
Some functions return both types, others return only one of them.

If you are using PDOStatement->fetch, notice the optional $fetch_style argument it takes.

滿滿的愛 2024-09-26 04:12:06

您的问题与 mysql_fetch_array 函数有关。

如果您只需要数字,请使用:

$row = mysql_fetch_array($result, MYSQL_NUM)

如果您只需要文本索引,请使用:

$row = mysql_fetch_array($result, MYSQL_ASSOC)

我通常使用 MySQLi,但对于 PDO,您可以在这里找到更多信息:https://www.php.net/manual/en/pdostatement.fetch.php

这似乎意味着您应该将其用于文本索引:

$row = $statement->fetch(PDO::FETCH_ASSOC);

这用于数字索引:

$row = $statement->fetch(PDO::FETCH_NUM);

Your issue is with the mysql_fetch_array function.

If you want only the numbers, use:

$row = mysql_fetch_array($result, MYSQL_NUM)

If you want only the text indexes, use:

$row = mysql_fetch_array($result, MYSQL_ASSOC)

I usually use MySQLi but for PDO you can find more information here: https://www.php.net/manual/en/pdostatement.fetch.php

Which seems to mean that you should be using this for text indexes:

$row = $statement->fetch(PDO::FETCH_ASSOC);

And this for numeric indexes:

$row = $statement->fetch(PDO::FETCH_NUM);
等风来 2024-09-26 04:12:06

另外,请注意这一点,因为它没有在这里列出,如果您想要一个关联数组,您可以使用 mysql_fetch_assoc(),或者如果您想要一个枚举(编号)数组,请使用 mysql_fetch_row() 而不是 mysql_fetch_array()。我使用这个主要是因为没有它我经常会忘记标志,所以我养成了只使用特定函数的习惯。

Also, just to note this since it isn't listed here, if you want an associative array, you can use mysql_fetch_assoc(), or if you want an enumerated (numbered) array use mysql_fetch_row() instead of mysql_fetch_array(). I use this mostly because without it I would often forget the flag, so I got into the habit of just using the specific function.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文