如何从我的mysql数据库中随机检索图像? [英] How to randomly retrieve images from my mysql database?

查看:83
本文介绍了如何从我的mysql数据库中随机检索图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,所以我制作了一个php文件来输出图像,这是输出页面的示例代码:

Okay, so I've made one php file to output the images this is the sample code for the output page:

mysql_connect(,, )或死(mysql_error());
mysql_select_db()或die(mysql_error());

mysql_connect (" "," "," ") or die(mysql_error()); mysql_select_db (" ") or die(mysql_error());

$query = mysql_query("SELECT * FROM store"); 
$number=mysql_num_rows($query); 

$result = mysql_query ("SELECT * FROM store ORDER BY RAND() LIMIT $number");

while ($row = mysql_fetch_assoc($result))     
{       
    echo '<img src=get.php?id=$row["id"]>';
}

img标记引用的get.php具有以下代码:

The get.php that the img tag is referring to has this code:

mysql_connect(,,)或死亡(mysql_error());
mysql_select_db()或die(mysql_error());

mysql_connect (" "," "," ") or die(mysql_error()); mysql_select_db (" ") or die(mysql_error());

 $id = addslashes ($_REQUEST['id']);


$query = mysql_query("SELECT * FROM store WHERE id= $id ");
$row = mysql_fetch_array($query);
$content = $row['image'];

header('Content-type: image/jpg');
 echo $content;

我得到的只是输出页面上的一系列撕裂页面图标。我本可以犯一个非常简单的错误,因为我仍在学习php。在此先感谢。

All I'm getting are a series of torn page icons on the output page. I could have made a very simple mistake seeing as how I am still learning php. Thanks in advance.

推荐答案

清理:

$result = mysql_query("SELECT * FROM store ORDER BY RAND()");

while($row = mysql_fetch_assoc($result)){       
    echo '<img src="get.php?id='.$row[id].'" />';
}

你也可以 echo mysql_error(); 查看你的mysql语句中是否有任何错误。

You can also echo mysql_error(); to see if there are any errors in your mysql statements.

你还应该使用mysql_real_escape_string()而不是addslashes()

You should also use mysql_real_escape_string() instead of addslashes()

或者考虑 PDO 是一个更安全的解决方案。

Or consider PDO for an even more secure solution.

要调试,请转到get.php?id = 1。如果你看到一个图像,get.php正在工作而主文件没有。

To debug, go to get.php?id=1. If you see an image get.php is working and the main file is not.

你确定get.php连接到数据库以及主文件?

Have you made sure that get.php connects to the database as well as the main file?

这篇关于如何从我的mysql数据库中随机检索图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆