PHP MySQL ebook
February 12, 2011 – 4:57 pm | No Comment

Hey everyone!!
With this section I hope to share with you resources which I have tried and valued
So you’re new to PHP and want to learn from scratch. Well I was in your position not too …

Read the full story »
SEO Tutorials

Need help with Search Engine Optimisation? This is the place to look for the latest tips and hints with the advice we offer. Keep an eye out here for the latest posts.

PHP Tutorials

Learn how to code with PHP to achieve your goal. In here I aim to show and teach you about little tips and tricks that should enhance your coding abilities

Flash Tutorials

Learn how to use Adobe Flash for your own purposes. Little tips and hints to help you along

Website Development

In here I aim to help you with developing your website from scratch. Come here for various tips and pieces of advice to help you build and improve your website.

Home » PHP Tutorials

PHP Links + images form MySQL DB

Submitted by on July 29, 2007 – 3:19 pmNo Comment

I know i was having troubles with this a while ago but as i found a way in a PHP book on how to solve it i thought i will share it.

lets say your db has a column for websites and another for images and you want to make a list that displays all the info in the db including the website as a link and an image. well here is how to do it:

PHP:
  1. $db = mysql_connect("localhost", "root", "");
  2. mysql_select_db("db_name");
  3. // -----------------------------------------------
  4.  
  5. $result = mysql_query("SELECT * FROM table order by 'id'")or die(mysql_error());
  6. $num_links = mysql_num_rows($result);
  7. for ($i=0; $i<$num_links; $i++)
  8. {
  9. //part 1
  10. $row= mysql_fetch_array($result);
  11. $id = $row["id"];
  12. $name = $row["name"];
  13. $email = $row["email"];
  14. $website = $row["website"];
  15. $description = $row["brief_description"];
  16. $image_dir = $row["image_dir"];
  17.  
  18. //part 2
  19. print "User Id : $id
  20. ";
  21. print "Name : $name
  22. ";
  23. print "E-mail : $email
  24. ";
  25. print "Website : <a href="http://$website">$website</a>
  26. ";
  27. print "brief description : $description
  28. ";
  29. print "image is <img src="$image_dir" alt="" />
  30.  
  31. ";
  32. }
  33. ?&gt;

As you see in part 1 we got the data from the database. this is all what we need for now.

In Part 2 we print the data out that we stored before as variables. so it will view a block containing id,name,email,website,brief description and an image.

For the link i used:

PHP:
  1. print "Website : <a href="http://$website">$website</a>
  2. ";

what this does is it writes website then makes a link command telling it to hyperlink to the website that we got from the variable.

the same with the image image tag with a variable for the actual image. so it looks like this:

PHP:
  1. print "image is <img src="$image_dir" alt="" />
  2. ";

I hope that helps someone.

VN:F [1.9.13_1145]
Rating: 10.0/10 (2 votes cast)
VN:F [1.9.13_1145]
Rating: +1 (from 1 vote)
PHP Links + images form MySQL DB, 10.0 out of 10 based on 2 ratings

Popularity: unranked [?]

Leave a comment!

Add your comment below, or trackback from your own site. You can also subscribe to these comments via RSS.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

This is a Gravatar-enabled weblog. To get your own globally-recognized-avatar, please register at Gravatar.