Jump to content

selecting one result at a time?


xwishmasterx

Recommended Posts

I'm trying to get add some pictures from a database, and wish to use these one at a time on a page.

Once I have selecting the images with my query (standard select from method), how can I specify these images one at a time? eg:

 

echo "$picture one"; blah blah echo "$picture two"; on so on?

Link to comment
Share on other sites

mysql_fetch_assoc moves the specified results' internal pointer forward one step on each call, so just keep calling mysql_fetch_assoc.

 

 

<?php
include 'db.php';
$query = "SELECT href, alt FROM images LIMIT 3";
$result = mysql_query($query);
$row = mysql_fetch_assoc($result);
?>
<img src="<?php echo htmlentities($row['href'], ENT_QUOTES, 'UTF-8'); ?>" alt="<?php echo htmlentities($row['alt'], ENT_QUOTES, 'UTF-8'); ?>" >
<?php $row = mysql_fetch_assoc($result); ?>
<img src="<?php echo htmlentities($row['href'], ENT_QUOTES, 'UTF-8'); ?>" alt="<?php echo htmlentities($row['alt'], ENT_QUOTES, 'UTF-8'); ?>" >
<?php $row = mysql_fetch_assoc($result); ?>
<img src="<?php echo htmlentities($row['href'], ENT_QUOTES, 'UTF-8'); ?>" alt="<?php echo htmlentities($row['alt'], ENT_QUOTES, 'UTF-8'); ?>" >

 

Alternatively, you could use mysql_result

<?php
include 'db.php';
$query = "SELECT href, alt FROM images LIMIT 3";
$result = mysql_query($query);
?>
<img src="<?php echo htmlentities(mysql_result($result, 0, 0), ENT_QUOTES, 'UTF-8'); ?>" alt="<?php echo htmlentities(mysql_result($result, 0, 1), ENT_QUOTES, 'UTF-8'); ?>" >
<img src="<?php echo htmlentities(mysql_result($result, 1, 0), ENT_QUOTES, 'UTF-8'); ?>" alt="<?php echo htmlentities(mysql_result($result, 1, 1), ENT_QUOTES, 'UTF-8'); ?>" >
<img src="<?php echo htmlentities(mysql_result($result, 2, 0), ENT_QUOTES, 'UTF-8'); ?>" alt="<?php echo htmlentities(mysql_result($result, 2, 1), ENT_QUOTES, 'UTF-8'); ?>" >

 

I am unsure of the speed implications of each method, I think mysql_fetch_assoc will be faster, but mysql_result looks cleaner IMO.

Link to comment
Share on other sites

thanks bunch Andy!

It seems I am in over my head here.lol

 

my problem is more complicated when I move forward, so the whole scenario comes to this:

I check for friends (friends_user_id) in one table, then need to grab the friends user_image in another table IF it exists..

Also, I need to do a "str_replace" for the user image.

 

I think I'm closing in on getting some freelance work here.lol

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.