Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用PHP检索MySQL列的一部分_Php_Mysql - Fatal编程技术网

使用PHP检索MySQL列的一部分

使用PHP检索MySQL列的一部分,php,mysql,Php,Mysql,例如,如果我有下表: +----+---+----------+ | id | a | position | +----+---+----------+ | 0 | 0 | 0 | | 1 | 0 | 1 | | 2 | 1 | 4 | | 3 | 1 | 9 | | 4 | 1 | 6 | | 5 | 1 | 1 | +----+---+----------+ 我想得到一个数组,它包含从a为1的位

例如,如果我有下表:

+----+---+----------+
| id | a | position |
+----+---+----------+
| 0  | 0 | 0        |
| 1  | 0 | 1        |
| 2  | 1 | 4        |
| 3  | 1 | 9        |
| 4  | 1 | 6        |
| 5  | 1 | 1        |
+----+---+----------+
我想得到一个数组,它包含从a为1的位置开始的前100个值,我该怎么做 我猜是这样的:

$col = mysql_fetch_array( mysql_query('
SELECT `position`
FROM `table`
WHERE `a`="1"
ORDER BY `position` ASC
LIMIT 100
'));
$result = mysql_query("SELECT index, position FROM table WHERE a = 1 ORDER BY position ASC LIMIT 100");

while($col = mysql_fetch_array($result)){
    *do something*
}
我希望得到以下数组:

+-------+-------+
| index | value |
+-------+-------+
| 0     | 1     |
| 1     | 4     |
| 2     | 6     |
| 3     | 9     |
+-------+-------+
但它不起作用。
?我该怎么做才能让它工作?
谢谢

mysql\u fetch\u array()
每次从查询结果中获取一行。要访问所有行,您需要一个循环。类似于

while ($row = mysql_fetch_array($result, MYSQL_NUM)) 
{
  printf("index: %s  value: %s", $row[0], $row[1]);  
}

我会仔细看看:

好的,有几件事:

在fetch_数组中运行mysql_查询很奇怪。Mysql_fetch_数组处理查询结果,将结果的各行(已获取)放入数组中。所以,当你按照你的方式运行它时,如果它运行了,它只会给你第一行,而不是前一百行

第二,这句话看起来很奇怪。根据“a”中的数据类型,双引号可能会导致错误。(有一点没有使用MySQL,可能是错误的。)

如果我要这样做,我会这样做:

$col = mysql_fetch_array( mysql_query('
SELECT `position`
FROM `table`
WHERE `a`="1"
ORDER BY `position` ASC
LIMIT 100
'));
$result = mysql_query("SELECT index, position FROM table WHERE a = 1 ORDER BY position ASC LIMIT 100");

while($col = mysql_fetch_array($result)){
    *do something*
}
*感谢JYelton对查询进行了重新格式化。

您的查询没有问题

问题是
mysql\u fetch\u array
只检索一行。您应该循环所有行,并将每个值添加到
$col
数组中

$result = mysql_query('...');

while($row = mysql_fetch_array($result, MYSQL_NUM))
{
    $col[] = $row[0];
}
现在,
$col
包含以下内容:

Array
(
    [0] => "1"
    [1] => "4"
    [2] => "6"
    [3] => "9"
)

您从该查询中获得了哪些结果而不是您想要的结果?这通常是可疑的。它不应该是='1'或=1吗?我得到一个数组,其中只有一个索引等于1。我怀疑=“1”是错的,因为我在MySQL中总是使用双引号,到目前为止我还没有遇到任何问题。