alconebay Posted August 26, 2008 Share Posted August 26, 2008 Here is what is going on. Users enter all of their dog's titles as comma separated values in a single text area and that information is entered into the database. No problem there. Later, I want to get that "titles" field and explode the titles and echo out information on each title. If that doesn't make and sense maybe the code will: <?php $titles_array = explode(", ", $titles); //breaks the comma separated values into pieces. ?> Now I want to go through the array and echo the name/description for each title (piece) in the database: <?php for($i = 0; $i < count($titles_array); $i++) { echo "Piece $i = $titles_array[$i] <br />"; $query="SELECT * FROM title WHERE title_name='$titles_array[$i]'"; $result=mysql_query($query); $title_name=mysql_result($result,$i,"title_name"); $title_description=mysql_result($result,$i,"title_description"); echo "Title Name: $title_name<br>"; echo "Title Name: $title_description<br>"; } ?> On the first loop, it works great. After that, I get this: Warning: mysql_result() [function.mysql-result]: Unable to jump to row 1 on MySQL result index 16 in *path* on line 20 What am I doing wrong? Link to comment https://forums.phpfreaks.com/topic/121436-looping-only-works-on-the-first-loop/ Share on other sites More sharing options...
akitchin Posted August 26, 2008 Share Posted August 26, 2008 i would assume that it's because you're only getting one row from the database during each query. in that case, only row 0 will exist. if you only intend to get one row per query, change mysql_result() to use 0 rather than $i. Link to comment https://forums.phpfreaks.com/topic/121436-looping-only-works-on-the-first-loop/#findComment-626221 Share on other sites More sharing options...
alconebay Posted August 26, 2008 Author Share Posted August 26, 2008 Works great now! Thanks Link to comment https://forums.phpfreaks.com/topic/121436-looping-only-works-on-the-first-loop/#findComment-626244 Share on other sites More sharing options...
Mchl Posted August 26, 2008 Share Posted August 26, 2008 Consider also removing query from the loop. You can prepare query before, and send it to database once. Then loop through the reults. Link to comment https://forums.phpfreaks.com/topic/121436-looping-only-works-on-the-first-loop/#findComment-626246 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.