我正在修復一個別人編碼的項目,這是一團糟!在mysql中存儲短數字的最佳方法
在編輯用戶個人資料頁面上,他們有類似下面的內容,其中將列出用戶可以選擇的用於教育級別的選項的所有可用無線電選擇框,這僅僅是1個項目,有許多像這樣的項目在這裏的答案,我可以將它應用於此代碼的許多其他功能。
他們用一個數組的許多個人資料欄,它會存儲可能的答案,用戶只能選擇1分的答案,雖然在這種情況下。所以對於教育來說,有7個可能的答案,數組中對應的數字存儲在一個mysql數據庫中,他們將這個數字作爲varchar 255長度存儲起來,這個長度不會太好。
我應該將其存儲爲1一個varchar的長度或使用枚舉並列出了7個選項,還是其他什麼東西?
教育
<?PHP
// Array of possible education profile selections
$arr_education[1]="No Answer";
$arr_education[2]="High school";
$arr_education[3]="Some college";
$arr_education[4]="In college";
$arr_education[5]="College graduate";
$arr_education[6]="Grad/professional school";
$arr_education[7]="Post grad";
// shows the array above as checkboxes on a form
foreach($arr_education as $ind=>$val) {
echo '<input type="radio" name="education" value="' .$ind. '" ' if($education==$ind){ echo "checked";}. '>' .$val. '<br>';
}
//on a users profile page there would be something like this
$education = 7; // seven would be Post Grad from our array above
echo $arr_education[$education]
//Now here is the mysql table, they chose to use a varchar(255) which seems retarded, using the above code, the max result would be 1 charachter long
education varchar(255)
?>
這與255的最大長度這不是存儲號碼的好方法,一個varchar但如果需要更少的長度,用得少。 – pavium 2009-09-03 21:14:39
@pavium:當MySQL在內存中創建臨時表(例如對結果集進行排序時),它將爲每行分配完整的255個字節,就像它是一個CHAR列一樣。 – 2009-09-04 00:47:58