[英]MySQL returning two rows with same data under different keys
MySQL returning two rows with same data under different keys. MySQL在不同的键下返回具有相同数据的两行。
Array
(
[0] => 1
[id] => 1
[1] => Good
[name] => Good
[2] => 1.jpg
[path] => 1.jpg
)
The thing i'm talking about is [0] and [id] have the same data and it returned directly from the query why is this and is there a way to fix it to just return the string key. 我在说的是[0]和[id]具有相同的数据,它直接从查询中返回,这是为什么呢,并且有一种方法可以将其修复为仅返回字符串键。
My query function. 我的查询功能。
<?php
class Database
{
private static $connection = null;
public function Connect()
{
self::$connection = mysqli_connect( "127.0.0.1", "root", "", "image");
if (mysqli_connect_errno()) { die("Failed to connect to MySQL"); }
}
public function custome($query)
{
$result = mysqli_query(self::$connection, $query);
while ($item = mysqli_fetch_array($result)) { $resultArray[] = $item; } return $resultArray[0];
}
}
?>
When you call mysqli_fetch_array
it defaults to MYSQLI_BOTH
which returns both numerical and associative indexes. 当您调用
mysqli_fetch_array
它默认为MYSQLI_BOTH
,它同时返回数字索引和关联索引。
You probably want either MYSQLI_NUM
or MYSQL_ASSOC
, not the default MYSQLI_BOTH
. 您可能需要
MYSQLI_NUM
或MYSQL_ASSOC
,而不是默认的MYSQLI_BOTH
。
Alternately, use mysqli_fetch_assoc
which defaults to an associative array. 或者,使用默认为关联数组的
mysqli_fetch_assoc
。
Here's the relevant documentation: 以下是相关文档:
use mysqli_fetch_assoc
instead of mysqli_fetch_array
to retrieve assocaitive(key/value) array 使用
mysqli_fetch_assoc
而不是mysqli_fetch_array
来检索关联(键/值)数组
public function custome($query)
{
$result = mysqli_query(self::$connection, $query);
while ($item = mysqli_fetch_assoc($result))
{
$resultArray[] = $item;
}
return $resultArray[0];
}
You can reference an array, called an "association", by identifying the key/value pair of the associative array. 您可以通过标识关联数组的键/值对来引用一个称为“关联”的数组。 The item location in the array is a number
myarray[n-1]
where n is the size of the array. 数组中的项目位置是数字
myarray[n-1]
,其中n是数组的大小。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.