2013-03-19 187 views
-2

我已經創建了一個名爲movie的數據庫,並且有名爲test_image(id int autoincrement,name varchar(30),image blob)的表。數據插入與PHP的幫助code.Now我已經顯示與下面的代碼幫助圖片:php + mysql圖像幻燈片


<?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); 

>


,僅顯示一個圖像 現在如果我?想要在數據庫中幻燈片的圖像我必須做什麼變化?

+0

很多變化。從不再使用'mysql_ *'函數開始。 – hjpotter92 2013-03-19 07:42:23

+0

[**請不要在新代碼中使用'mysql_ *'函數**](http://bit.ly/phpmsql)。他們不再被維護[並被正式棄用](https://wiki.php.net/rfc/mysql_deprecation)。看到[**紅框**](http://j.mp/Te9zIL)?學習[*準備的語句*](http://j.mp/T9hLWi),並使用[PDO](http://php.net/pdo)或[MySQLi](http://php.net/ mysqli) - [這篇文章](http://j.mp/QEx8IB)將幫助你決定哪個。如果你選擇PDO,[這裏是一個很好的教程](http://www.brightmeup.info/article.php?a_id=2)。 – 2013-03-19 07:43:02

回答

-1

簡單的步行扔結果設置:

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

請把您使用obsolte功能。請使用mysqli的(http://php.net/manual/de/book.mysqli.php

+2

fyi pdo會比mysqli好 – 2013-03-19 07:43:45

+0

從下次我肯定會用mysqli_而不是mysql_ 這顯示錯誤:undefined index 0 – user2060304 2013-03-19 07:50:42

0

你在你的代碼所缺少一點

的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']\">"; 
} 
?> 
0

爲images.I的幻燈片現在我已經包括JS已經editted我這樣的代碼:

<?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> 

但是這個代碼不顯示圖像......

+0

可以找到爲什麼這段代碼沒有顯示圖片 – user2060304 2013-03-21 05:23:05