-2

i have created a database named movie and there is table named test_image(id int autoincrement,name varchar(30),image blob). Data is inserted with the help of php code.Now i have displayed images with the help of following code:


<?php
$host="yourhostname";
$user="username";
$pass="password";
$db="movie";

// just so we know it is broken
error_reporting(E_ALL);

//connect to the db
$link = mysql_connect("$host", "$user", "$pass") or die("Could not connect: " . mysql_error());

// select our database
mysql_select_db("$db") or die(mysql_error());

// get the image from the db
$sql = "SELECT image FROM test_image;";

// the result of the query
$result = mysql_query("$sql") or die("Invalid query: " . mysql_error());

// set the header for the image
header("Content-type: image/jpeg");

echo mysql_result($result, 0);

// close the db link
mysql_close($link);

?>


it shows only one image now if i want to slideshow of images in database what changes i have to make??

user2060304
  • 11
  • 2
  • 5
  • A lot of changes. Starting from not using `mysql_*` functions anymore. – hjpotter92 Mar 19 '13 at 07:42
  • [**Please, don't use `mysql_*` functions in new code**](http://bit.ly/phpmsql). They are no longer maintained [and are officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). See the [**red box**](http://j.mp/Te9zIL)? Learn about [*prepared statements*](http://j.mp/T9hLWi) instead, and use [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli) - [this article](http://j.mp/QEx8IB) will help you decide which. If you choose PDO, [here is a good tutorial](http://www.brightmeup.info/article.php?a_id=2). – NullPoiиteя Mar 19 '13 at 07:43

3 Answers3

0

you are missing a bit in your code

mysqli

<?
//no more mysql_ means start using mysqli
$mysqli = new mysqli('host', 'username', 'password', 'database'); //connect
$query = "SELECT image FROM test_image"; // your query
$result = $mysqli->query($query); // Fetch Result
//now do something with that result
foreach ($result as $row){
   echo "<img src=\"$row['image']\">";
}
?>
Grmn
  • 542
  • 2
  • 9
0

Now I have included js for slideshow of images.I have editted my code like this:

<?php
include "file_constants.php";//in this file connection is established with db
header('Content-Type: text/html; charset=utf-8');
$sql="SELECT * FROM test_image;";
$result=mysqli_query($con,$sql) or die("invalid query ".mysqli_error());
$array=array();
while($row=mysqli_fetch_array($result))
{
$array[]=$row['image'];
}
 ?>


           <html>
           <body>

    <title>Slideshow</title>
    <h1>Slideshow of images stored in mysql</h1>
    <script language="javascript">
        var delay=1000; 
        var curindex=0;


        var randomimages=["$array[0]","$array[1]","$array[2]","$array[3]","$array[4]","$array[5]"];

        var preload=[],img,curindex,tempindex;

        for (var n=0;n<randomimages.length;n++)
        {
            img=new Image();
            img.src=randomimages[n];
            preload.push(img);
        }

        document.write('<img name="defaultimage" src="'+randomimages[Math.floor(Math.random()*(randomimages.length))]+'">');

        function rotateimage()
        {
            if (curindex==(tempindex=Math.floor(Math.random()*(randomimages.length))))
            {
                curindex=curindex==0 ? 1 : curindex-1;
            } else
            {
                curindex=tempindex;
            }

            document.images.defaultimage.src=randomimages[curindex];
        }

        window.setInterval(rotateimage,delay);
    </script>
</body>
     </html>

but this code does not show image......

Bartłomiej Semańczyk
  • 59,234
  • 49
  • 233
  • 358
user2060304
  • 11
  • 2
  • 5
-1

Simple walk throw the result set with:

while($row = mysql_fetch_assoc($result)){
    echo $row[0];
}

Please regard that you use obsolte functions. Please use mysqli (http://php.net/manual/de/book.mysqli.php)

tschiela
  • 5,231
  • 4
  • 28
  • 35