我在stackexchange上發佈了下面的代碼審查。但我只是意識到我所要求的可能是SO的合法問題。 如果您不這麼認爲,請讓我知道。php腳本從sql表中檢索枚舉值
我有一個SQL表有多個字段,其中4個是枚舉。我編寫了一個腳本,它運行思想表並檢索枚舉並將它們放入2維數組中。
不幸的是,這個腳本速度很慢,我無法修復它。
<?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);
?>