Linked Questions

Popular Questions

Display BLOB data from SQLITE database using PHP

Asked by At

Hope You all have a Great Day, I new with SQLITE DB and so I'm little confused about handling BLOB data.I tried lot of things ,but nothing get in to favor of me.

Here is My Code

INSERTING DATA

insert.php

<form method="POST" enctype="multipart/form-data">
        model no:<input type="text" name="model_no"\><span style="color: red"><?php echo $messages["model_no"]; ?></span>
        image:<input type="file" name="image" id="image"\><span style="color: red">
        <input type="submit" value="save" id="save" name="save"/>
        <input type="reset" value="Clear"/>
    </form>

include.php

<?php
    $flag = 0;
    class MyDB extends SQLite3
    {
        function __construct()
        {
            $this->open('../database.db');
        }
    }

    //checking save button is clicked
    if(isset($_POST["save"])){
        $model_no = $_POST['model_no'];
        $image = $_FILES['image']['name'];

        if($model_no != '' && $image != ''){
            $flag = 1;
        }

        if($flag == '1'){
            $db = new MyDB();

            if(!$db){
                echo $db->lastErrorMsg();
            } 
            else{
                    $dbb = new MyDB();

                    $sql = 'SELECT COUNT(*) as count FROM fisfis WHERE model_no =  "'.$model_no.'"';

                    //echo $sql;

                    $rows = $dbb->query($sql);
                    $row = $rows->fetchArray();
                    $numRows = $row['count'];
                    //echo $numRows;

                    if($numRows != 0){
                        $flag = 0;
                        echo "error:this model no is already taken";
                    }else{
                        $sqlp = "INSERT INTO fisfis(model_no, image) values('$model_no','$image')";

                        if($dbb->exec($sqlp)){
                            echo "data inserted successfully\n";
                        }
                        else{
                            echo"error:\n";
                            echo "data not inserted \n";
                            echo"contact admin for details\n";
                        }
                    }

           }
        }
        else{
                        echo"error:\n";
                        echo "data not inserted : data fields cannot be empty\n";
            }

    }   
?>

this is very successfully inserting data in to my table (table name:fisfis)

DISPLAYING DATA

display.php

<form method="POST" enctype="multipart/form-data">
        <p>Enter Barcode : <input type="text" name="search_model" id="search_model" /></p> 
        <!--<p><img src='image.php?id=<?php //echo $row['model_no'];?>'/></p>-->
        <p><input type="submit" value="search" name="search" id="search"/></p>
    </form>

display_include.php

<?php

    $flag2 = 0;
    $flag3 = 0;

    class MyDB extends SQLite3
    {
        function __construct()
        {
            $this->open('../database.db');
        }
    }

    $db = new MyDB();

    if(isset($_POST["search"])){
        $model_no = '';
        $model_no = $_POST['search_model'];

        if($model_no != ''){
            $flag2 = 1;
        }

        if($flag2 == '1'){
            $db = new MyDB();

            if(!$db){
                echo $db->lastErrorMsg();
            } 

            else{
                $dbb = new MyDB();

                $sql = 'SELECT COUNT(*) as count FROM fisfis WHERE model_no =  "'.$model_no.'"';

                $rows = $dbb->query($sql);
                $row = $rows->fetchArray();
                $numRows = $row['count'];
                echo $numRows;

                if($numRows == 0){
                    echo 'sorry this model no is not exists';
                    echo '<br/>';
                    echo '<a href="insert_controller.php">create new</a>';
                    $flag2 = 1;
                }else{
                    echo "this is exisists";
                    $flag3 = 1;
                }

                if($flag3 == 1){
                    $sqla = 'SELECT * FROM fisfis WHERE model_no =  "'.$model_no.'"';
                    $result = $dbb->query($sqla);

                    while($row = $result->fetchArray(SQLITE3_ASSOC) ) {

                        echo "MODEL NO = ". $row['model_no'] ."\n";

                        $img = '\'<img src="'. $row['image'] .'" width="100" height="100"/>\'';
                        echo $img;

                   }

                }

            }

        }
    }


?>

the display part displaying all data other than blob ,what i have tried

1.try to echo the image inside the php script

$img = '\'<img src="'. $row['image'] .'" width="100" height="100"/>\'';
  1. try to display image inside html tag

but this is not working,please help me to fix this,thanks in advance

Related Questions