2014-09-03 44 views
-1

我正在學習PHP/MySQL,同時在Web應用程序上工作。部分應用程序要求從名爲「htdocs/full」的圖像目錄回顯隨機圖像。除了'first_name''last_name'的列之外,數據庫保存標題爲'image_path'的列中每個圖像的路徑。如何從PHP中隨機抽取MySQL圖像?

我可以讓它從數據庫中調用一個隨機的行,我可以讓它顯示來自'first_name'和'last_name'列的文字數據。但我努力使它從'image_path'列轉換成圖像而不是文字。

我在過去的24小時裏一直在嘗試一些方法,但是我在Stack和Google上看到的大多數其他方法都是直接從目錄中提取圖像而不使用數據庫行(它由於相應的'first_name'和'last_name')。

這裏是我到目前爲止(雖然我也有許多不同的變化,這個我覺得是我來最接近):

<?php 
    $username="root";$password="*********";$database="offenders"; 

    mysql_connect('localhost',$username,$password); 

    mysql_select_db($database) or die("Unable to select database"); 

    $sSQLQuery = "SELECT image_path FROM offenders ORDER BY RAND() LIMIT 1"; 
    $aResult = mysql_query($sSQLQuery); 
    WHILE($aRow = mysql_fetch_array($aResult)): 
    { 
    header("Content-Type:image/jpeg"); 
    echo "<img src='php/imgView.php?imgId=".$aRow['image_path']."' />"; 
    } 

    ENDWHILE; 

?> 

它生成一個網頁,在一個破碎的圖像左上方。我已經嘗試過使用$ aRow以及$ aRow ['image_path']的回顯,以及其他一些似乎會導致錯誤的調整。任何想法我做錯了什麼?

非常感謝!

更新代碼:

<?php 
     $username="root";$password="*******";$database="offenders"; 

     mysql_connect('localhost',$username,$password); 

     mysql_select_db($database) or die("Unable to select database"); 

      $src="full/"; 

      //if you have any problem in the solution check this line, 
//means weather you are providing right path for your files or not. 


     $sSQLQuery = "SELECT image_path FROM offenders ORDER BY RAND() LIMIT 1"; 
     $aResult = mysql_query($sSQLQuery);      
     $aRow = mysql_fetch_array($aResult);     


     $file_path=$src.$aRow["image_path"]; 
     //echo "<a href='php/imgView.php?imgId=".$aRow['image_path']."'>"."<img src=".$file_path." />"."</a>".'<br/>'; 
      echo "<img src=".$file_path." />".'<br/>'; 
      //echo "<img src=full/1b2ba2f9a8b8ee9c062b09767535d69bd954e125.jpg>"; 



    /* if(isset($_GET['imgId'])) 
     { 
    //i am query only first_name but you can add for field as you want 
    //and i also used $_get['imgId']; in the query which is not safe but you can make it for //secure. 

     $sSQLQuery = "SELECT first_name FROM offenders where image_path='".$_GET['imgId']."'"; 
     $aResult = mysql_query($sSQLQuery); 
     WHILE($aRow = mysql_fetch_array($aResult)): 
     { 
     echo $aRow['first_name']; 
     } 

     ENDWHILE; 
     } 
     */ 
    ?> 
+0

你怎麼看,當你查看從Web瀏覽器生成的HTML源? – iWantSimpleLife 2014-09-03 01:02:19

+0

Ack。添加適當的標籤:<!DOCTYPE HTML> 罪犯 user3246092 2014-09-03 01:08:27

+0

確定。從你的來源看,PHP似乎是正確的。我想你應該檢查你的imgView.php tp確保它正確地發送圖像數據。 – iWantSimpleLife 2014-09-03 03:46:56

回答

0

有幾件事情錯在這裏。

  • 你有你的數據庫輸出循環。取而代之的

    WHILE($ AROW = mysql_fetch_array($ aResult)): { ... }

    ENDWHILE;

使用:

$aRow = mysql_fetch_array($aResult); 
... 
  • 您正在返回什麼樣子的HTML,但你有JPG的內容類型。返回HTML並使用適當的內容類型或返回JPG,然後返回。
  • 如果您想要返回JPG本身(而不是HTML),請使用CURL來獲取寫入圖像並將其返回。
0

在我看來我不知道你爲什麼運行while循環,因爲在你的查詢中你有一個限制,所以你不必運行while循環。

可以說你必須在文件夾名在圖像的PHP /圖像/比你的圖像來

我寫我的,前提是你的圖像在圖像文件夾,如果他們不是你的解決方案可以簡單地更改解決方案中的$ src值。

我也想過你想用這些圖片作爲鏈接,所以你可以得到first_name和last_name,當你點擊圖片時,原因是我這樣想,你試着給id img src =「」,

任何方式我正在寫兩個解決方案,看看哪一個爲你工作。第一個會讓他們鏈接,第二個只會從數據庫中拉出圖像。

首先假設你想從數據庫中拉出first_name和last_name,當點擊一下圖片

<?php 
     $username="root";$password="";$database="offenders"; 

     mysql_connect('localhost',$username,$password); 

     mysql_se 

lect_db($database) or die("Unable to select database"); 

      $src="php/image/";//if you have any problem in the solution check this line, 
//means weather you are providing right path for your files or not. 


     $sSQLQuery = "SELECT image_path FROM offenders ORDER BY RAND() LIMIT 1"; 
     $aResult = mysql_query($sSQLQuery);      
     $aRow = mysql_fetch_array($aResult);     


     $file_path=$src.$aRow["image_path"]; 
     echo "<a href='php/imgView.php?imgId=".$aRow['image_path']."'>"."<img src=".$file_path." />"."</a>".'<br/>'; 



     if(isset($_GET['imgId'])) 
     { 
    //i am query only first_name but you can add for field as you want 
    //and i also used $_get['imgId']; in the query which is not safe but you can make it for //secure. 

     $sSQLQuery = "SELECT first_name FROM offenders where image_path='".$_GET['imgId']."'"; 
     $aResult = mysql_query($sSQLQuery); 
     WHILE($aRow = mysql_fetch_array($aResult)): 
     { 
     echo $aRow['first_name']; 
     } 

     ENDWHILE; 
     } 
    ?> 

第二溶液只從數據庫

拉圖像去除isset條件和同時在isset條件循環。

和改變這一行

echo "<a href='php/imgView.php?imgId=".$aRow['image_path']."'>"."<img src=".$file_path." />"."</a>".'<br/>'; 

echo "<img src=".$file_path." />".'<br/>'; 

    //if you notice the only different is this that i 
//remove the <a> tag in its path, so not it gonna pull images 
// from the database but you cant use them as link. 

感謝

這個解決方案正常工作,如果它不請把你的文件夾結構,意味着您擁有的圖像和文件imgView。和第二記我刪除從溶液中while循環,

//

+0

嘿阿里夫。感謝您的幫助。它一直沒有工作,並仍然在頁面上提供了一個破碎的圖像圖標 - 我不明白,因爲你給我的一切都非常有意義。我將路徑更改爲$ src =「full /」;.我還添加了回聲「」;測試路徑是否正常工作。我不明白你的代碼爲什麼不起作用。這一切都是有道理的。我很困惑,但謝謝 – user3246092 2014-09-05 21:57:07

+0

full/1b2ba2f9a8b8ee9c062b09767535d69bd954e125.jpg - 計算機/ C:/ MAMP/htdocs - localhost/stack.php - 編輯源代碼的原始文章 – user3246092 2014-09-06 22:29:21

+0

完整版與isset沒有註釋掉沒有工作作爲好,澄清 – user3246092 2014-09-06 22:31:33