Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/239.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\u fetch\u数组向前移动内部指针_Php_Mysql - Fatal编程技术网

Php 使用mysql\u fetch\u数组向前移动内部指针

Php 使用mysql\u fetch\u数组向前移动内部指针,php,mysql,Php,Mysql,我有一个类似以下内容的表(第一行是列): 使用一些代码检查节值是否与数组中的某个值匹配: $sectionscope = Array('bananas', 'apples'); $sections = mysql_query("SELECT section FROM table WHERE col1=val AND col2=val2"); if (mysql_num_rows($sections)) { $i = 0; while ($row = mysql_fetch

我有一个类似以下内容的表(第一行是列):

使用一些代码检查节值是否与数组中的某个值匹配:

$sectionscope = Array('bananas', 'apples');

$sections = mysql_query("SELECT section FROM table WHERE col1=val AND col2=val2");

if (mysql_num_rows($sections)) {

    $i = 0;

    while ($row = mysql_fetch_array($sections)) {

        if (in_array($row[$i], $sectionscope)) {

            $section = $sectionscope[array_search($row[$i], $sectionscope)];

            $section_is_valid = 1;

        }

        $i++;

    }

}
如果我使用echo$row[$I]回显while循环的输出,它将给出:
bananas

从PHPmyadmin中进行选择工作正常

你能告诉我我做错了什么吗? 谢谢


SystemError

为什么要在while循环中增加
$i
。您的查询将返回两行,当您在结果中循环时,每次都可以使用
$row[0]
访问
部分

while ($row = mysql_fetch_array($sections)) {
echo $row[0];    
}
这将打印香蕉和桃子


在您的代码中,当您第二次输入while循环时,您正试图使用
$row[1]
检索
节的值(因为您的
$i
已增加到1),该值将为空,因为您的查询结果只包含一列。

为什么不首先查询您想要的节呢,典型的逻辑错误。我一直认为,当这些字段不是行时,行是在索引号上迭代的。谢谢
while ($row = mysql_fetch_array($sections)) {
echo $row[0];    
}