PDO将数组作为行获取

时间:2016-03-26 14:11:47

标签: php mysql pdo

您好我正在尝试将代码从旧的MSQL转换为PDO,以便我可以从数据库上传和查看图像,我收到以下错误代码

  

注意:数组转换为字符串   第60行的C:\ xampp \ htdocs \ Pentaslash \ includes \ addpost.php

如果我将fetchAll更改为只是获取它将不会识别row [2]数据库具有以下行id(PK)name(imagename)image(longblob)

<html>
    <body>
    <form method="post" enctype="multipart/form-data">
        <br/>
        <input type="file" name="image" />
        <br/><br/>
        <input type="submit" name="submit" value="Upload"/>
        </form>
        <?php
            if(isset($_POST['submit'])) 
            {
                if(getimagesize($_FILES['image']['tmp_name']) == FALSE)
                {
                    echo "Please Select an image";
                }
            else
            {
                $image= addslashes($_FILES['image']['tmp_name']);
                $name= addslashes($_FILES['image']['name']);
                $image= file_get_contents($image);
                $image= base64_encode($image);
                saveimage($name,$image);
            }
            }
        displayimage();
        function saveimage($name,$image)
        {
            try {
            define('SITE_ROOT', dirname(__FILE__));
            require SITE_ROOT .  '\dbconnect.php';
            $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // <== add this line
            $sql="INSERT INTO images (name,image) values ('$name','$image')";
                if ($conn->query($sql)) {
                    echo "uplaoded";
                }
                else{
                    echo "failed";
                }

            $conn = null;
            }
            catch(PDOException $e)
            { 
            echo $e->getMessage();
            } 
        }
        function displayimage()
        {
            try {

            require SITE_ROOT . '\dbconnect.php';
            $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            $result = $conn->prepare("SELECT * FROM images");
            $result->execute(); 
            while ($row = $result->fetchAll(PDO::FETCH_ASSOC)) {
                echo '<img height="300" width="300" scr="data:image;base64,' .$row[2]. '">';

            }

            $conn = null;
            }
            catch(PDOException $e)
            { 
            echo $e->getMessage();
            } 
        }
        ?>



    </body>
</html>

3 个答案:

答案 0 :(得分:2)

抓取时你做错了。 fetchAll返回结果行的数组。所以你要做的就是迭代数组:

$resultRows = $result->fetchAll(PDO::FETCH_ASSOC);
foreach($rows as $row){
  echo '<img height="300" width="300" scr="' .$row['image']. '">'; // you can also use $row[2]
}

另一种可能性是:

if ($result->execute(array())) {
  while ($row = $result->fetch()) {
    echo '<img height="300" width="300" scr="' .$row['image']. '">'; // you can also use $row[2];
  }

答案 1 :(得分:2)

使用fetchAll()和foreach循环,你可以试试这个:

    $rows = $result->fetchAll();    
    foreach($rows as $row):
     echo '<img height="300" width="300" scr="data:image;base64,' .$row['image']. '">';
    endforeach;

如果您仍想使用while循环,则应该是:

 while($rows = $result->fetch(PDO::FETCH_ASSOC)){
      echo '<img height="300" width="300" scr="data:image;base64,' .$row['image']. '">';
}

答案 2 :(得分:0)

好的,对于将来遇到这个问题的用户来说,这里有这个问题, 首先,我的错误在于我没有像其他人那样在上面提到foreach循环。 其次我的SCR语句错误,这是显示base64图像的代码的正确部分

Set
相关问题