PDA

View Full Version : Displaying binary images on pages from mysql?


Andy92
05-07-2006, 10:45 AM
Hi there!

On my site, my friend has made a news managment system, so that through mysql you can easily post news. Go to http://www.allsortshop.com/news to see.

In the /news folder i have index.php which is the main file, and article.php whcih is to display articles.

In the index.php, this is the php code that gets all the news from the database...

<?php
mysql_connect("localhost", "username", "password") or die(mysql_error());
mysql_select_db("database") or die(mysql_error());
$result = mysql_query("SELECT * FROM news WHERE type=0 LIMIT 0 , 10");
if ($row2 = mysql_fetch_array($result)) {
echo "<table>\n";
do {
Printf("<tr><td><b>%s</b></td></tr><tr><td>%s <a href=article.php?read=$row2[id]>more...</a></td></tr><tr><td>&nbsp;</td></tr>", $row2[title], $row2[mini], $row2[id]);
}while ($row2 = mysql_fetch_array($result));
}else{
echo "No News... Shocking!!!";
}
echo "</table>\n";
?>

That displays news files, but it only displays an intro, then when you click more it goes to the full news article at /news/article.php?read=1

(in the article.php ive converted the id to read)

Then once on the page article.php?read=1 or read=2 depending on what id number the news article is in mysql, it then shows you the full story.

In the article.php this is the code at the very top of that page before the <html> tag...

<?php
$read = $_GET['read'];
mysql_connect("localhost", "username", "password") or die(mysql_error());
mysql_select_db("database") or die(mysql_error());
$result = mysql_query("SELECT * FROM news WHERE id=$read");
$text = mysql_fetch_array($result);
?>

Then in the <body</body> tags of the article.php page i have...

<?php
echo ("<b>".$text['title']."</b>");
echo "<p>";
echo $text['full'];
?>

Now in the mysql this is my news table...

http://www.allsortshop.com/images/mysql.JPG


The thing is, i want to display all the images like image.php?id=1 or whatever and then on the news page, when you click more on an article and it takes you to article.php?read=1 then on the mysql table, on the row with id = 1 in it, it has an image at the end. I want to display that image in the article.php?read=1. And then on article.php?read=2, i want it to display the image on that one too which will be located at image.php?id=2.

How do i do this and make the image.php file?

:confused: :confused: :confused:

GJay
05-07-2006, 10:50 AM
http://www.devarticles.com/c/a/MySQL/Blobbing-Data-With-PHP-and-MySQL/5/
is a tutorial outlining one approach.
You would have:
<img src="getfile.php?id=imageid" />
in the html, and it would display the image.

Another approach, is to grab the image from the database, and save it to the file-system, then refer to that as you would to a normal image- this has the advantage that you can employ caching, to avoid pulling the image on every single page load.

Andy92
05-10-2006, 05:02 PM
When i compolite that tutorial, with a test database, it doesn't work?

Is there any other tutorials like that one that i can try?

Beagle
05-10-2006, 05:50 PM
You don't compolite PHP.

What errrors are you getting when you run it. If you're not getting error messages, you need to put:
ini_set("display_errors","1");
at the top of your page.

What are your errors? That tutorial does it correctly.

Andy92
05-17-2006, 05:05 PM
Don't worry everything has been sorted out now.

Thanks!