-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathpopulate_column.php
More file actions
92 lines (63 loc) · 2.51 KB
/
populate_column.php
File metadata and controls
92 lines (63 loc) · 2.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
<?php
require_once(__DIR__ . '/util/mysqli.php');
require_once(__DIR__ . '/vendor/autoload.php');
if($argv[0] == basename(__FILE__)){ //then this script was called from the command line directly...
if(!isset($argv[4])){
echo "Usage: populate_column.php {database} {table} {column} {faker_field}";
exit();
}
$database = f_mysql_real_escape_string($argv[1]);
$table = f_mysql_real_escape_string($argv[2]);
$column = f_mysql_real_escape_string($argv[3]);
$faker_field = f_mysql_real_escape_string($argv[4]); //who knows perhaps there will be malicous input to the php code?
$is_check_with_user = true;
populate_column($database,$table,$column,$faker_field,$is_check_with_user);
}
function populate_column($database,$table,$column,$faker_field,$is_check_with_user = false){
if(
strtolower($faker_field) == 'latin' ||
strtolower($faker_field) == 'lorem' ||
strtolower($faker_field) == 'ipsum'){
$faker_field = "paragraph";
}
$faker = Faker\Factory::create();
$sql = "
SELECT
COUNT(DISTINCT(`id`)) AS distinct_id_count,
COUNT(*) AS row_count,
COUNT(DISTINCT($column)) AS distinct_row_count
FROM $database.$table
";
$result = f_mysql_query($sql);
$row = mysqli_fetch_assoc($result);
$row_count = $row['row_count'];
$distinct_row_count = $row['distinct_row_count'];
$distinct_id_count = $row['distinct_id_count'];
if($distinct_id_count != $row_count){
echo "ERROR: This script assumes that it can use the id field in $database.$table as a unique identifier to update each field... but it looks like that will not work\n";
echo "row_count: $row_count distinct_row_count: $distinct_row_count distinct_id_count: $distinct_id_count\n";
echo "based on the output from \n $sql \n\n ";
exit();
}
$answer = 'yes'; //assume this for when $is_check_with_user = false;
if($is_check_with_user){
echo "There are $row_count rows of data and $distinct_row_count distinct values in those data currently living inside $database.$table column $column. \n";
$answer = readline("Given that, are you sure you want to continue? (needs a 'yes') >");
}
if(strtolower($answer) == 'yes'){
for($i=1; $i <= $row_count ; $i++){ //loop over every row of data
$new_value = f_mysql_real_escape_string($faker->$faker_field);
$update_sql = "
UPDATE $database.$table
SET $column = '$new_value'
WHERE id = '$i'
";
echo "adding $new_value\n";
f_mysql_query($update_sql);
}
}
}
//someday we might want to change how this works.
function esc($string){
return "'". f_mysql_real_escape_string($string)."'";
}