xwishmasterx Posted June 10, 2012 Share Posted June 10, 2012 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? Quote Link to comment https://forums.phpfreaks.com/topic/263961-selecting-one-result-at-a-time/ Share on other sites More sharing options...
floridaflatlander Posted June 10, 2012 Share Posted June 10, 2012 Can you use while ($row = mysqli_fetch_array($rp, MYSQLI_ASSOC)){ echo '<a title="'.$row['title'].'" href="'.$plink.''.$row['pid'].'">'; } Quote Link to comment https://forums.phpfreaks.com/topic/263961-selecting-one-result-at-a-time/#findComment-1352750 Share on other sites More sharing options...
xwishmasterx Posted June 10, 2012 Author Share Posted June 10, 2012 Sorry, but can't use a while loop for output, as the pictures are added different places. Is there a way, to create a variable for each value? like: $pic1=first value; $pic2=second value; etc. ? Quote Link to comment https://forums.phpfreaks.com/topic/263961-selecting-one-result-at-a-time/#findComment-1352751 Share on other sites More sharing options...
Andy-H Posted June 10, 2012 Share Posted June 10, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/263961-selecting-one-result-at-a-time/#findComment-1352752 Share on other sites More sharing options...
xwishmasterx Posted June 10, 2012 Author Share Posted June 10, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/263961-selecting-one-result-at-a-time/#findComment-1352764 Share on other sites More sharing options...
Andy-H Posted June 10, 2012 Share Posted June 10, 2012 Use a join, sorry I can't help more but it's 12:10am so I need to get my sleep for work. I'll help tomorrow (well, later today) if it's still not resolved Quote Link to comment https://forums.phpfreaks.com/topic/263961-selecting-one-result-at-a-time/#findComment-1352765 Share on other sites More sharing options...
Pikachu2000 Posted June 11, 2012 Share Posted June 11, 2012 Or just dump all of the results into an array, and use them as needed. $storage = array(); while( $array = mysql_fetch_assoc($result) ) { $storage[] = $array; ) Quote Link to comment https://forums.phpfreaks.com/topic/263961-selecting-one-result-at-a-time/#findComment-1352796 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.