0

I have some php script that I would like to run on EVERY row within a mySQL table. The php script:

function replacespaces($text) {
    return preg_replace_callback('/[\<]pre(.*)[\>](.*)[\<]\/pre[\>]/i', 
        create_function(
            '$matches',
            'return "<pre".$matches[1].">".str_replace(" ", " ", $matches[2])."</pre>\n";'
        ), $text);
}

I need to cycle through the mySQL database in the table "jos_picmicro_content" passing the content of the field "fulltext" to the above function, and inserting the result back into the field "fulltext". Sounds easy enough to do, although I am not experienced in the area! The table is about 500 rows long.

1
  • Is that code an example? Because it replaces a space with a space. Commented Jul 16, 2011 at 8:08

1 Answer 1

2
$result = mysql_query("SELECT * FROM jos_posmicro_content");
while ($row = mysql_fetch_array($result)) {
    $text = replacespaces($row['fulltext']);
    $sql = "UPDATE jos_posmicro_content SET fulltext = '" . mysql_real_escape_string($text) . "' WHERE ????";
    mysql_query($sql);
}

You just ned to change the ???? part to identify which row to update (eg, where id = $row['id'] or whatever your primary key is).

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you Dan. I am not familar with the mysql side of things. What do you mean by "primary key"?
How would you pick the right row from the table for whatever purpose it's storing data? How do you tell one row from another? I'm not sure how you even managed to write the query to create this table without knowing what a primary key is...
Figured as much, I did some googling while waiting for your response. Thanks again Dan.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.