Log in

View Full Version : Fetch Images From URLs in MYSQL



EMT
03-04-2010, 08:09 AM
I have a mysql database set up and have uploaded image paths and various text fields to it. The images are being sent and stored in a folder. What I need help with is how to code my php page correctly to display the images and working links.

'log_filename' is the image field
'bands_website' is the link field

My code:



<?php
// Make a MySQL Connection
mysql_connect("") or die(mysql_error());
mysql_select_db("") or die(mysql_error());

// Insert a row of information into the table "_uploads_log"
$result = mysql_query("SELECT * FROM _uploads_log WHERE genre LIKE 'Country' ")
or die(mysql_error());

// keeps getting the next row until there are no more to get
while($row = mysql_fetch_array( $result )) {
// Print out the contents of each row

// Print out the contents of each row into a table
echo " Video: ".$row['log_filename'];echo "<br />";
echo " Member Name: ".$row['login'];echo "<br />";
echo " Band Name: ".$row['band_name'];echo "<br />";
echo " Title: ".$row['title'];echo "<br />";
echo " Description: ".$row['description'];echo "<br />";
echo " Prior Artist: ".$row['prior_artist'];echo "<br />";
echo " Home Town: ".$row['home_town'];echo "<br />";
echo " Web Site: ".$row['bands_website'];echo "<br />";
echo " State: ".$row['state'];echo "<br />";
echo " Genre: ".$row['genre'];echo "<br />";
echo " Posted: ".$row['date'];echo "<br />";echo "<br />";
}

?>


Thanks in advance for any help. EMT

djr33
03-04-2010, 08:12 AM
I don't understand the difficulty.

You just need to echo the URL to the image in an <img src="...."> tag and the URL to the website in an <a href="..."> tag.

Is there something else that is not working?

EMT
03-04-2010, 08:24 AM
Thanks for answering so fast. I am going to use this on my site for the "Related Videos" section on my members pages. I wont know the addresses and that' s why I'm setting up a database to handle it. I have just started this and when I'm finished I want to have all files uploaded this way...EMT

djr33
03-04-2010, 08:42 AM
Yes, I understand. You should be able to copy this line:
echo " Posted: ".$row['date'];echo "<br />";echo "<br />";

And do whatever HTML you need, such as:
echo "<a href=\"".$row['URLFIELD']."\">LINKNAME</a>";

If you need help with this formatting, then an introduction to PHP strings tutorial would be a good idea.

In short:
echo makes things print into the html.
Quotes (either double or single) surround literal text.
Variables can be output as well.
You can combine any strings into one with a . between them.
If you need to use quotes (double/single) within the same kind of quotes (double/single) surrounding the string, you must "escape" them like this:
'can\'t' or "<a href=\"index.htm\">Home</a>"
In both of those examples the inner quotes are disabled by the slashes.

EMT
03-04-2010, 10:29 AM
I got those both to work.

My Newest Code:

[CODE]
<?php
// Make a MySQL Connection
mysql_connect("localhost", "database", "password") or die(mysql_error());
mysql_select_db("database") or die(mysql_error());

// Insert a row of information into the table "_uploads_log"
$result = mysql_query("SELECT * FROM _uploads_log WHERE genre LIKE 'Country' ")
or die(mysql_error());

// keeps getting the next row until there are no more to get
while($row = mysql_fetch_array( $result )) {
// Print out the contents of each row

// Print out the contents of each row into a table
echo "<img src=\"folderwhereimagesaresentto/".$row['log_filename']."\" width=92 height=62 hspace=0 vspace=0 border=0></a>";echo "<br />";
echo " Member: ".$row['login'];echo "<br />";
echo " Band: ".$row['band_name'];echo "<br />";
echo " Title: ".$row['title'];echo "<br />";
echo " Description: ".$row['description'];echo "<br />";
echo " Prior Artist: ".$row['prior_artist'];echo "<br />";
echo " Home Town: ".$row['home_town'];echo "<br />";
echo "<a href=\"".$row['bands_website']."\">Web Site</a>";echo "<br />";
echo " State: ".$row['state'];echo "<br />";
echo " Genre: ".$row['genre'];echo "<br />";
echo " Posted: ".$row['date'];echo "<br /><br />";
}

?>
[CODE]

Thank You...ET

EMT
03-05-2010, 11:17 PM
Hello again, I'm having a problem with displaying a link. I want my my 'login' data to display and link to my 'bands_website' data.

My Code is:

[CODE]
<LINK REL=STYLESHEET TYPE="text/css" HREF="../../../../site.css">
<?php
// Make a MySQL Connection
mysql_connect("host", "database", "password") or die(mysql_error());
mysql_select_db("database") or die(mysql_error());

// Get all the data from the "myydatabase" table
$result = mysql_query("SELECT * FROM _uploads_log")
or die(mysql_error());
echo "<table border='0' cellspacing='6' cellpadding='0'>";
echo "<tr align='left'></tr>";
// keeps getting the next row until there are no more to get
while($row = mysql_fetch_array( $result )) {
// Print out the contents of each row into a table
echo "<tr><td>";
echo "<img src=\"path/".$row['log_filename']."\" width=92 height=62 hspace=0 vspace=0 border=0></a>";echo "</td><td valign='middle'>";
echo "Title: ";echo $row['title'];echo "<br />";
echo "Band: ";echo $row['band_name'];echo "<br />";
echo "Member: ";echo $row['login'];echo "<br />";
echo "Description: ";echo $row['description'];echo "<br />";
echo "Member: ";echo "<a href=\"".$row['bands_website']."\">I WANT THIS TO BE THE 'login' DATA NOT TEXT FOR A LINK</a>";echo "<br />";
echo $row['date'];
echo "</td><td>";

}

?>
[CODE]

Thank You...EMT

EMT
03-06-2010, 01:49 AM
I looked around and pieced it together.

[CODE]
echo "Member: ";echo "<a href=".$row['bands_website'] . ">" .$row['login'] . "</a>";echo "<br />";
[CODE]

EMT

djr33
03-06-2010, 06:42 PM
Ok, good. If you still have trouble with this, then an introductory PHP tutorial should cover the questions about strings (text output), and this is a good tutorial for learning how to use mysql:
http://php-mysql-tutorial.com

(The layout is confusing: the oldest tutorials are the most basic so go to the last page of tutorials and start with the "last" one, even though that's really the first. At the moment that's: http://www.php-mysql-tutorial.com/wikis/Default.aspx?PageIndex=2 )

EMT
03-07-2010, 04:15 AM
OK, Thanks for your help. I will check out the links you provided. I'm now trying to link an image, then I will be done with this part and can use it on my site.

EMT
03-07-2010, 06:27 AM
OK, this is driving me crazy. Can you help me with the above problem. I want to make the image that's displayed from the 'log_filename' field a link by using the data from the 'bands_website' (which is a url).

[CODE]
<LINK REL=STYLESHEET TYPE="text/css" HREF="../../../../site.css">
<?php
// Make a MySQL Connection
mysql_connect("host", "database", "passwd") or die(mysql_error());
mysql_select_db("myydatabase") or die(mysql_error());

// Get all the data from the "myydatabase" table
$result = mysql_query("SELECT * FROM _uploads_log")
or die(mysql_error());
echo "<table border='0' cellspacing='6' cellpadding='0'>";
echo "<tr align='left'></tr>";
// keeps getting the next row until there are no more to get
while($row = mysql_fetch_array( $result )) {
// Print out the contents of each row into a table
echo "<tr><td>";
echo "<img src=\"path/".$row['log_filename']."\" width=92 height=62 hspace=0 vspace=0 border=0></a>";echo "</td><td valign='middle'>";
echo "Title: ";echo "<a href=".$row['bands_website'] . ">" .$row['title'] . "</a>";echo "<br />";
echo "Band: ";echo $row['band_name'];echo "<br />";
echo "Description: ";echo $row['description'];echo "<br />";
echo "Member: ";echo "<a href=".$row['bands_website'] . ">" .$row['login'] . "</a>";echo "<br />";
echo "Posted: ";echo $row['date'];
echo "</td><td>";

}


?>
[CODE]

Thanks again in advance for any help...EMT

EMT
03-07-2010, 06:31 AM
To further clarify, the line that I want to edit is:

[CODE]
echo "<img src=\"path/".$row['log_filename']."\" width=92 height=62 hspace=0 vspace=0 border=0></a>";echo "</td><td valign='middle'>";
[CODE]

Thank You...EMT

EMT
03-07-2010, 07:52 AM
Hell again, This is the closest I can get.

[CODE]
echo "<img src=\"mypath/".$row.">" .$row['bands_website']."\"></a>";
[CODE]

It displays an image place holder and of course doesn't link.

I know the <a href has to also be in there somehow. PLEASE HELP...EMT

EMT
03-07-2010, 11:53 AM
I Figured it out.

[CODE]
echo "<a href=\"" . $row["bands_website"] . "\"><img src=\"IMAGEPATH/" . $row["log_filename"] . "\" border=0 alt=\"" . $row["text"] . "\"></a>";
[CODE]

EMT