Jump to content

looping only works on the first loop.


alconebay

Recommended Posts

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

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.

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.