我在 stackexchange 上发布了以下内容以进行代码审查。但我刚刚意识到我要问的问题对于 SO 来说可能是一个合法的问题。如果您不这么认为,请告诉我。
我有一个包含多个字段的 sql 表,其中 4 个是枚举。我编写了一个脚本,运行该表并检索枚举并将它们放入二维数组中。
不幸的是,这个脚本非常慢,我无法修复它。
<?php
require_once('mySQL_Connect.php');
$con = ConnectToDataBase();
if ($con == false)
{
//data returned will be null
exit;
}
$db = 'courses_db';
$table = 'courses';
$fields = array(
'training_field',
'speciality_field',
'type',
'language');
$enums = array();
foreach ($fields as $colomn) {
$sq1 = "SELECT
column_type
FROM
information_schema.columns
WHERE
table_schema = '$db'
AND
table_name = '$table'
AND
column_name = '$colomn'";
$query = mysqli_query($con,$sq1);
$stack = array();
$i = 0;
$stack[$i]=$colomn;
if ($fetch = mysqli_fetch_assoc($query) )
{
$enum = $fetch['column_type'];
$off = strpos($enum,"(");
$enum = substr($enum, $off+1, strlen($enum)-$off-2);
$values = explode(",",$enum);
// For each value in the array, remove the leading and trailing
// single quotes, convert two single quotes to one. Put the result
// back in the array in the same form as CodeCharge needs.
for( $n = 0; $n < Count($values); $n++) {
$val = substr( $values[$n], 1,strlen($values[$n])-2);
$val = str_replace("''","'",$val);
$stack[$i+1]=$val;
$i++;
}
}
// return the values array to the caller
//echo json_encode( $stack);
array_push($enums,$stack);
reset($stack);
}
echo json_encode($enums);
?>