Skip to content

Instantly share code, notes, and snippets.

@dcnl1980
Created August 6, 2016 14:40
Show Gist options
  • Save dcnl1980/f2abcbc28b5a5129f290af46872c34f6 to your computer and use it in GitHub Desktop.
Save dcnl1980/f2abcbc28b5a5129f290af46872c34f6 to your computer and use it in GitHub Desktop.
Create Slugs in large MYSQL tables
<?php
// This script must be run on a command line
error_reporting(E_ALL);
set_time_limit(1200);
date_default_timezone_set("Europe/Amsterdam");
$mysqli = new mysqli('localhost', 'username', 'password', 'table');
if ($mysqli->connect_error) {
die('Error : ('. $mysqli->connect_errno .') '. $mysqli->connect_error);
}
print_r('Progressing...');
$res = $mysqli->query('SELECT id, name FROM companies WHERE slug is NULL');
print_r($res);
while($obj = $res->fetch_object()){
$slug = iconv('utf-8', 'us-ascii//TRANSLIT', $obj->name);
$slug = strtolower(preg_replace('/[^A-Za-z0-9-]+/', '-', $slug));
$slug = trim($slug, '-'); // Remove the last - if applicable
$mysqli->query('UPDATE companies SET slug="'.$slug.'" WHERE id = '.$obj->id.'');
}
$res->free(); // Free the memory
$mysqli->close(); // Close the connection
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment