Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/244.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/64.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 Pdo中的变量未定义_Php_Mysql_Arrays_Pdo - Fatal编程技术网

Php Pdo中的变量未定义

Php Pdo中的变量未定义,php,mysql,arrays,pdo,Php,Mysql,Arrays,Pdo,我在pdo中有一个数组,它不显示任何输出,比如varibale图像不定义Variable$pic不定义。。。。 下面是代码请告诉我哪里错了 $rows = $result->fetch(PDO::FETCH_NUM); if(count($rows) > 0) { while($row=$result->fetch(PDO::FETCH_OBJ)){ $image = $row['file']; $pic = array('1.jpg','2.jpg',

我在pdo中有一个数组,它不显示任何输出,比如varibale图像不定义Variable$pic不定义。。。。 下面是代码请告诉我哪里错了

$rows = $result->fetch(PDO::FETCH_NUM);
if(count($rows) > 0) {
    while($row=$result->fetch(PDO::FETCH_OBJ)){
    $image = $row['file'];
    $pic = array('1.jpg','2.jpg','3.jpg',$image);
    }
    }
我使用for循环回显此数组,但变量定义错误

 <?php 
  for($i=0;$i<4;$i++)
   echo"<option  data-img-src=\"$pic[$i]\" value=\"$pic[$i]\" ></option>";

    ?>

的范围内定义,而
循环。在顶部声明它,它就会工作。旧版本的php可以工作,但最新版本给出了例外

  $pic="";
  $rows = $result->fetch(PDO::FETCH_NUM);
  if(count($rows) > 0) {
    while($row=$result->fetch(PDO::FETCH_OBJ)){
    $image = $row['file'];
    $pic = array('1.jpg','2.jpg','3.jpg',$image);
    }
  }

您正在使用
FETCH_OBJ
样式,然后将输出当作数组使用,从而导致错误

这可能有两种方式:

  • 将提取样式更改为
    fetch\u ASSOC
    ,以便可以将其用作数组

    while($row=$result->fetch(PDO::FETCH_ASSOC)){
    $image = $row['file'];
    
  • 或者更改
    $image
    行,使其使用
    $row
    变量作为对象

    while($row=$result->fetch(PDO::FETCH_OBJ)){
    $image = $row->file;
    

这不起作用,我只为$image编写数组,并在中获取$image的数据,那么如何从循环外部写入/