Results 1 to 9 of 9
  1. #1
    Join Date
    Oct 2002
    Posts
    32

    Unanswered: Search and Replace

    I know how to connect to a MySQL database, and I know the basic PHP search-and-replace syntax is: $haystack= str_replace("needle", "new_needle", $haystack).

    What do I need to do to use this code to search and replace all needles with new_needles in one column ($field_name) of my MySQL database? Thanks!

    Timm

  2. #2
    Join Date
    Sep 2002
    Location
    Kyiv, Ukraine
    Posts
    77
    First you should connect to MySQL database using mysql_connect() function, then launch a query using mysql_query() function to get the information about column you need to change and after all this simly asking for every record from this resulted query (using mysql_fetch_array() in a while loop) you should launch another query on the raw table with UPDATE instruction and desired variables ...
    Yours faithfully,
    Yaroslav Zaremba

  3. #3
    Join Date
    Oct 2002
    Posts
    32
    I'm still missing something. The connect and query functions work fine. The following fetch array function correctly prints the changes that I want to make in the database:

    while ($row = mysql_fetch_array($sql_result)) {
    $FieldToChange = $row["FieldToChange"];
    $FieldToChange = str_replace("Old", "New", $FieldToChange);
    Print $FieldToChange;
    }

    When I change the code as follows to update the MySQL table, however, I get a parse error on the UPDATE line below:

    while ($row = mysql_fetch_array($sql_result)) {
    $FieldToChange = $row["FieldToChange"];
    UPDATE MyTableName
    SET $FieldToChange = str_replace("Old", "New", $FieldToChange);
    Print $FieldToChange;
    }

    What am I doing wrong?

    Thanks!

    Timm

  4. #4
    Join Date
    Sep 2002
    Location
    Kyiv, Ukraine
    Posts
    77
    Do not understand your PHP code at all! ... You shoul do like this (if everything is OK with your query and connection to DB):

    while ($row = mysql_fetch_array($sql_result)) {
    $FieldToChange = $row[FieldToChange];
    $FieldToChangeID = $row[FieldToChangeID];
    $FieldToChange = str_replace("Old", "New", $FieldToChange);
    mysql_query("UPDATE MyTableName
    SET FieldToChange = $FieldToChange
    WHERE FieldToChangeID = $FieldToChangeID;");

    }
    Yours faithfully,
    Yaroslav Zaremba

  5. #5
    Join Date
    Oct 2002
    Posts
    32

    Getting search and replace to work with text?

    Thanks, aZa. I am a step closer, but something is still missing.

    I'm using the following line to do a search and replace of the word "Old" with the word "New":
    $FieldToChange = str_replace("Old", "New", $FieldToChange);

    I have discovered that if "Old" and "New" are numeric (i.e., "123" and "456", the code works. Every instance of "123" is updated to "456."

    However, if the search and replace items are text (i.e., "Old search item" and "New replacement item"), the code does not work. Is there something else I need to do to make this work with text?

    Thanks!

    Timm
    Last edited by Timm; 01-28-03 at 11:23.

  6. #6
    Join Date
    Oct 2002
    Posts
    32

    Thanks, aZa!

    aZa,

    I posted this problem on the php.com site and was told to single-quote my variables in the UPDATE . . . SET . . . statement. This resolved the problem I was having with searching and replacing text. Thanks for all your help!

    Timm

  7. #7
    Join Date
    Sep 2002
    Location
    Kyiv, Ukraine
    Posts
    77
    Yep, that's right, single-quotes are the missed things. Sorry about mistake.
    Yours faithfully,
    Yaroslav Zaremba

  8. #8
    Join Date
    Oct 2002
    Posts
    32

    One record not updating

    Okay, I thought I was finished, but I found one more problem. When I use the following code to do a search and replace, all records but the last record that was previously updated are successfully updated. What do I need to do to get this last record to update, too?

    while ($row = mysql_fetch_array($sql_result)) {
    $SearchField1 = $row[$SearchField];
    $Sequence = $row["Sequence"];
    $SearchField2 = str_replace($SearchFor, $ReplaceWith, $SearchField1);
    mysql_query("Update TableName
    SET $SearchField = '$SearchField2'
    WHERE Sequence = '$Sequence'");
    }

    Thanks!

    Timm

  9. #9
    Join Date
    Sep 2002
    Location
    Kyiv, Ukraine
    Posts
    77
    Should work with last record as well. Check your "outbound" code from what you provided here on forums ...
    Yours faithfully,
    Yaroslav Zaremba

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •