Image link in Mysql

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Testor
Forum Newbie
Posts: 12
Joined: Tue Jul 16, 2002 11:26 am

Image link in Mysql

Post by Testor »

Hi,

Working on a quiz script, wanted to add images to certain questions that need an image to clarify things. So, not all questions have images associated with them.

in the db(mysql) I added a field in the questions table and called it image, in this field I put the link to the images ex: /images/figure1.gif

My problem is that I'm unable to come up with the proper mysql query nor that I'm able to make the if statement present the image if there's one in the certain question row for example.

if($image){
echo "/images/$image";}

the mysql query, I don't know whether I shoould fetch row, num rows or what?

A quick run code would be very appreciated as I exausted everything. Nothing on the net, and certainly I'm new to this.

Thanks.
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

Well, depending on what else you have stored, I can only give you the basic examples...

Code: Select all

<?php
// get 5 questions (rows)
$result = mysql_qyery("select question, image from table where id = '1' limit 5");

while ($row = mysql_fetch_array($query)) {
 // the question will of course be there i assume...
 echo 'Question: '. $row[0];
 // is the next value empty? if not = image = print it...
 if (!empty($row[1])) { echo '<img src="'.$row[1].'">'; }
}
Hope it gave you some help.
Testor
Forum Newbie
Posts: 12
Joined: Tue Jul 16, 2002 11:26 am

Post by Testor »

Jam,

Thanks much, it certainly got me started, I worked on it for a while. First nothing happened, but then I got it to display the image but with every question :lol:

Yes I do have other stuff in the table like questionid, the question text, answers etc.. the image field is actually in row[10].

I do have a $query that selects from the questions table and that works fine.

Your code seems to be the one to do the trick I just have to keep working on it.
Post Reply