PHP Tutorial: Mass search and replace database field contents

Leave a comment Standard

So recently I needed to remove a bunch of URLs from my member’s content on my games because of spyware/malware notifications Chrome users were getting. It took me a bit to figure out how to do it so here’s a nice and nifty little script that will hopefully save you the same trouble.

<?php
/******
* Purpose: Mass search/replace all database fields
* Author: design1online.com, LLC
* License: GNU
******/

//connect to your database
$host = "localhost";
$username = "your_username_goes_here";
$pass = "your_pass_goes_here";
$database = "your_database_name_goes_here";

//the values you want to find/replace
$find = "what_you_want_to_find";
$replace = "what_you_want_to_replace_it_with";

//spit out the SQL without running it
$test_mode = true; //change this to false and the queries will be run automatically

//if you want to limit the replace to specific tables uncomment this line and enter them below
//$tables_list = array("table_name_1", "table_name_2", "table_name_3");

if ($tables_list)
    $limit_tables = " AND table_name IN ('" . explode("', '", $tables_list) . "')";

//loop through all the tables and all the columns and change $find to $replace
$loop = mysql_query("
    SELECT
        concat('UPDATE ',table_schema,'.',table_name, ' SET ',column_name, '=replace(',column_name,',''/{$find}/'',''/{$replace}/'');') AS s
    FROM 
        information_schema.columns
    WHERE
        table_schema = '{$database}'
        {$limit_tables}")
or die ('cannot loop through database fields: ' . mysql_error());

while ($query = mysql_fetch_assoc($loop))
{
    if ($test_mode)
        echo "{$query['s']}<br/>";
    else
        mysql_query($query['s']);
}
Advertisements

Flash AS3 Tutorial: How to Create 2D or 3D Dynamic, Multi-Dimensional Arrays

Comments 6 Standard

So creating a dynamic 2-dimensional array in flash isn’t as easy as you’d think. Instead of being able to just assign variables into a specific row/column position you have to initialize an array of arrays. Below are  code snippets to help you init 2D and 3D arrays.

Creating a 2D Array

var multiDimensionalArray:Array = new Array();
var boolArray:Array;
var MAX_ROWS = 5;
var MAX_COLS = 5;

//initalize the arrays
for (var row = 0; row <= MAX_ROWS; row++)
{
    boolArray = new Array();

    for (var col = 0; col <= MAX_COLS; col++)
        boolArray.push(false);
    }

    multiDimensionalArray.push(boolArray);
}

//now we can set the values of the array as usual
for (var row = 0; row <= MAX_ROWS; row++)
{
    for (var col = 0; col <= MAX_COLS; col++)
        boolArray[row][col] = true;
        trace('boolArray ' + row + ',' + col + ' = ' + boolArray[row][col]);
    }
}

Creating a 3D Array

var multiDimensionalArray:Array = new Array();
var boolArray:Array;
var anotherBoolArray:Array;
var MAX_X = 5;
var MAX_Y = 5;
var MAX_Z = 5;

//initalize the arrays
for (var x = 0; x <= MAX_X; x++)
{
    boolArray = new Array();

    for (var y = 0; y <= MAX_Y; y++)
    {

        anotherBoolArray = new Array();

        for (var z = 0; z <= MAX_Z; z++)
            anotherBoolArray.push(false);

        boolArray.push(anotherBoolArray);
    }

    multiDimensionalArray.push(boolArray);
}

//now we can set the values of the array as usual
for (var x = 0; x <= MAX_X; x++)
{
    for (var y = 0; y <= MAX_Y; y++)
    {
        for (var z = 0; z <= MAX_Z; z++)
        {
            boolArray[x][y][z] = true;
            trace('boolArray ' + x + ',' + y + ',' + z + ' = ' + boolArray[x][y][z]);
        }
    }
}