1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

php mysql mysql_fetch_array options

Discussion in 'MySQL' started by glasglow, Jun 29, 2007.

  1. #1
    I am using two columns in a table and am using this code to try to loop the results however this only makes changes on the last row. I have tried reorganizing the loop but don't seem to be getting it right. Is there another "fetch" option or should I change the coding itself? I want the str_replace to include all rows in the database and make replacements for each one not just one at a time so it needs to loop through the database and match all rows that it finds a match for. I thought I could just match the $newstring but it doesn't seem to be working... I tried putting all of the equation inside the loop but then it gives me three different outputs with still only 1 change each output. I need all the changes combined into one output.


    $string = $_POST['data'];

    while($row = mysql_fetch_array( $result )) {
    $newstring = str_replace($row[one],$row[two],$string);
    }
     
    glasglow, Jun 29, 2007 IP
  2. glasglow

    glasglow Active Member

    Messages:
    926
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    60
    #2
    Here is what I am using now.. but I am still getting only the last row.. I need all the rows.

    $result=mysql_query("select * from TABLE");

    while($row=mysql_fetch_array($result)){
    $matchArray = $row[COLUMN];
    $replaceArray = $row[COLUMN];
    }

    $newstring = str_replace($matchArray,$replaceArray,$string);
    echo $_POST['data'];
    echo $newstring;

    This is from a post.. It posts to the script.. the script matches text from the post and then gives me both the old text and the changed text. I am trying to use instead of a hard coded array()... a databases for the array values where it will match and replace all values from the database if they exist.
     
    glasglow, Jun 29, 2007 IP
  3. Transcendent

    Transcendent Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Well, your echo() statement needs to be in the loop...
     
    Transcendent, Jun 30, 2007 IP
  4. glasglow

    glasglow Active Member

    Messages:
    926
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    60
    #4

    I've tried it but the results I'm getting are not exactly what I want. If I put the echo() in the loop then I get this:


    1:This is NEW one This is two this is three

    2:This is one This is NEW two this is three

    3:This is one This is two this is NEW three

    But what I want to get is:

    1:This is NEW one This is NEW two This is NEW three.
     
    glasglow, Jun 30, 2007 IP
  5. UnrealEd

    UnrealEd Peon

    Messages:
    148
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #5
    try this:
    $string = $_POST['data'];
    
    while($row = mysql_fetch_array( $result )) {
    $string = str_replace($row[one],$row[two],$string);
    }
    PHP:
    you assigned the modified string to a new variable, named $newstring. So when looping over the resultset, you change the current $row[one], and assigns the changed string to a new variable ($newstring). What you had to do was update the current variable ($string).
     
    UnrealEd, Jun 30, 2007 IP
    glasglow likes this.
  6. glasglow

    glasglow Active Member

    Messages:
    926
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    60
    #6
    UnrealEd.. rep added.. thank you SOOOOO much. The logic... it's almost unthinkable.. (A NOT = B) BUT (A+B) = A...
     
    glasglow, Jun 30, 2007 IP