While loop problem

hey guys, having a strange problem with this loop.
the function basically randomises some text i send to it. for some reason its is not making the text random when I add to my db its all the same. but when I echo the text out at the end of each loop it is random.
something wrong with my query?

while($row = mysql_fetch_assoc($result)){

$myspuntext = "";
echo "done 1<BR>";
$myspuntext = spin($text);

mysql_query("UPDATE locations SET mytxt = '$myspuntext'");
echo $myspuntext;
echo "<BR><BR>";

}

Glad to hear it’s working. If you’re updating the same table that you’re fetching with mysql_fetch_assoc(), you can use:

mysql_query(“UPDATE locations SET mytxt = '”.mysql_real_escape_string($myspuntext).“’ WHERE ref=”.$row[‘ref’]);

That way you don’t have to manually increment an id variable to count through the rows.

If I understand correctly, you’re trying to get the text out of the row, spin it and put it back?

This problem relates you’re query:

mysql_query(“UPDATE locations SET mytxt = ‘$myspuntext’”);

What that instruction says is “Update all rows in locations by setting mytxt to $myspuntext”

What you need is a WHERE clause.

Something like this would do if you have given your rows ID’s:
mysql_query(“UPDATE locations SET mytxt = ‘$myspuntext’ WHERE id=”.$row[‘id’]);
Which in plain English is “Update rows in locations an id matching $row['id] by setting mytxt to $myspuntext”;

In the code below change $dbc with your database connection variable

mysql_query("UPDATE locations SET mytxt = '$myspuntext'", $dbc) or die(mysql_error());

Okay, so there’s something syntactically wrong with your sql query. I’d hazard a guess that you have single quotes (apostrophes) in your text and they’re causing your query to become invalid like this:
“UPDATE locations SET mytxt = ‘You’re in for a surprise if you run this query.’”

This is why I suggested echoing a failing query, so you can see what is really being sent to the server. You need to make sure you escape certain characters, especially single quotes. Try this:
mysql_query(“UPDATE locations SET mytxt = '”.mysql_real_escape_string($myspuntext).“'”)

Could you give an input/output example of this function, and echo out the query so we could see the final instruction given to the DB?

Also, being able to see a little more of the code may be helpful.

Try setting

mysql_query("UPDATE locations SET mytxt = '$myspuntext'") or die(mysql_error());

and it might return an error to explain why

k sorry guys, here is the error given ur error checking code.

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 's.

We have a huge array of colours and designs. If you like what you ’ at line 1

here is more of the code with function.

the function takes random text with {} swperated with | symbol

So I’ve wrote say 100 words and I’m having the sentences alternate each time it loops and then applying that to a text feild in the database… then doing the same with the next row…

while($row = mysql_fetch_assoc($result)){

$myspuntext = "";
echo "done 1<BR>";
$myspuntext = spin($text);

mysql_query("UPDATE locations SET mytxt = '$myspuntext'") or die(mysql_error());
echo $myspuntext;
echo "<BR><BR>";

}

function spin($text){
return preg_replace_callback(
‘~{([^}]+)}~’,
create_function(
‘$match’,
‘$possible = explode(“|”, $match[1]); return $possible[array_rand($possible)];’
),
$text
);
}

Am i using the query the wrong way? its doing what its mean to do but applying the same text to every row in my table instead of having semi random ones.

Jesus, it took a bit of tinkering to get it to work but here what I did, maybe a little long winded but still. Thanks so much for both of your help :slight_smile:

$row = mysql_fetch_array($result);

$id = 2;
while($row = mysql_fetch_assoc($result)){

$myspuntext = "";

$text = "

The{quick|slow|fast}{Brown|red|Green|Fox} was to {Quick|Slow} for the Hare
";

$myspuntext = spin($text);
//mysql_query("UPDATE locations SET mytxt = '".mysql_real_escape_string($myspuntext)."'",$connection) or die(mysql_error());
//mysql_query("UPDATE locations SET mytxt = ' '");
//echo $myspuntext;
mysql_query("UPDATE locations SET mytxt = '".mysql_real_escape_string($myspuntext)."' WHERE ref=".$id);

Print_r ($row);
Echo "<BR>$id";
//echo $row[1];
echo "<BR><BR><HR>";
unset($myspuntext);
$id = $id + 1;

}

function spin($text){
return preg_replace_callback(
‘~{([^}]+)}~’,
create_function(
‘$match’,
‘$possible = explode(“|”, $match[1]); return $possible[array_rand($possible)];’
),
$text
);
}

?>

I made those change but it is still filling all rows with the same text, when I echo it out thought its different. I’ve lost the error msg now though :slight_smile:

This does just work on the current row doesn’t it?

mysql_query(“UPDATE locations SET mytxt = ‘$myspuntext’”) or die(mysql_error());

*Little typo. “Update rows in locations with an id matching $row['id] by setting mytxt to $myspuntext”;

having looked at my database, the very last row it fills in is the same as the very last one echoed out. so for some reason the query seems to be filling in the whole column with the same data instead of getting new text for each run

SgtLegand, thanks for taking so much time helping me sort this out.

I’ve added that code but its still the same :frowning:

Not really sure what else to try? I’ve used the same code for another script which does exactly the same thing but with numbers and works.

In the while loop add this to the end of the while loop after your last line break echo

unset($myspuntext);