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
https://forums.phpfreaks.com/topic/263961-selecting-one-result-at-a-time/
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.

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

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.