php - 将数据库中的图像显示到结果页面

标签 php mysql

用户是否可以输入“红色汽车”,它会在数据库和 php/html 中找到该图像,并在搜索结果中显示该图像?

我的“搜索”代码

<?php
    $query = $_GET['query']; 


    $min_length = 3;


    if(strlen($query) >= $min_length){ // if query length is more or equal minimum length then

        $query = htmlspecialchars($query); 
        // changes characters used in html to their equivalents, for example: < to &gt;

        $query = mysql_real_escape_string($query);
        // makes sure nobody uses SQL injection

        $raw_results = mysql_query("SELECT * FROM articles
            WHERE (`title` LIKE '%".$query."%') OR (`text` LIKE '%".$query."%')") or die(mysql_error());

        // * means that it selects all fields, you can also write: `id`, `title`, `text`
        // articles is the name of our table

        // '%$query%' is what I'm looking for, % means anything, for example if $query is Hello
        // it will match "hello", "Hello man", "gogohello", if you want exact match use `title`='$query'
        // or if you want to match just full word so "gogohello" is out use '% $query %' ...OR ... '$query %' ... OR ... '% $query'

       if(mysql_num_rows($raw_results) > 0){ // if one or more rows are returned do following

    while($results = mysql_fetch_array($raw_results)){
    // $results = mysql_fetch_array($raw_results) puts data from database into array, while it's valid it does the loop



    echo "<div class='successmate'><h2></h2></a>

    </div><br><br><br>";

echo "<div class='search69'><a href='../pages/{$results['page_name']}'><h2>{$results['title']}</h2></a><p>{$results['text']}</‌​p>";



    }


}
    else{ // if there is no matching rows do following
    echo ("<br><br><div class='search1'><h2>No results</h2></br></br>");
}

}

else{ // if query length is less than minimum
echo ("<br><br><div class='search1'><h2>Minnimum Length Is</h2><h2>".$min_length);
}

?>

我想通过关键字查找的图片:

http://puu.sh/cCHv1/9f58d770f3.jpg

这些是数据库中图像的名称:

http://puu.sh/cCHwa/a82d2cc7fe.png

谢谢!

最佳答案

未经测试,我想说您已经关闭了搜索功能,显示结果将是一件简单的事情,只需将下面的代码放在您想要显示图像结果的位置(这仅用于显示图像,但是让它与其他结果一起工作是一个简单的返工)

<?php
while($data = $result->fetch_assoc()){
    echo '<img src="'.$data['image_path'].'" alt="" title="" />';
}
?>

然后,如果您愿意,您可以简单地将图像包装在任何容器中,另一种方法是将结果存储到'数组中并使用 foreach 循环显示它

<?php
while($data = $result->fetch_assoc()){
   $imageArray[] = $data['image_path'];
}

/*Code below you can place where ever you want, no need to place it directly after the 
 above query execute*/

foreach($imageArray as $imgPATH){
   echo '<img src="'.$imgPATH.'" alt="" title="" />';
}
?>

关于php - 将数据库中的图像显示到结果页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26729687/

相关文章:

php - 使用 PHP 从 Linux 服务器连接到远程 MS SQL 数据库

php - 如何为产品类别设置 SQL 表结构?

mysql - 数据库设计——主键命名约定

php - 任务详细信息已保存到数据库中,但任务之间的链接详细信息未保存

带条件的 PHP MySQL 查询

php - 使用php错误列出和删除mysql中的记录

php - CodeIgniter:帮助解决此查询

php - html_entity_decode 未按预期工作

php - 如何在 php pdo 中使用 2 个表获取 1 个表用户详细信息?

php - 缓存以减少负载(PHP 和 MYSQL)