[英]Can I get value from mysql table and set it as variable?
我有圖片中的 attribute_description :
在表上我有cap_tabel
值: d, lt, lu, amb, coada
我想獲取它們並使其可變並將值設置為等於attribute_id。
例如:
$d = 1;
$lt = 2;
$coada = 9;
我想我可以創建一個function.php
文件,在其中創建這個變量並在需要的地方使用它們。
這可能嗎,或者我必須創建一個腳本,我自己命名變量並將其設置為正確的值,例如:
$amb_ext = mysqli_fetch_array(mysqli_query($connect,"SELECT attribute_id FROM attribute_description WHERE cap_tabel='amb'"));
$amb = $amb_ext['attribute_id'];
你認為最好的解決辦法是什么?
最后,我想創建一個產品表,其中th
將是來自cap_tabel
和td
的值,以使用正確的th
單元格獲得正確的值
您可以使用 可變變量:
$res = mysqli_query($connect,"SELECT attribute_id, cap_tabel FROM attribute_description");
while ($row = $res->fetch_assoc()) {
$var = $row['cap_tabel'];
$$var = $row['attribute_id'];
}
// you now would have variables $d, $lt, etc
$result = mysqli_query($connect,"SELECT attribute_id, cap_tabel FROM attribute_description");
while ($aValue = mysqli_fetch_array($result)) {
${$aValue['cap_tabel']} = $aValue['attribute_id'];
}
echo ' $d = ' . $d . ', $lt = ' . $lt . ', $coada = ' . $coada;
要得到這樣的結果:
$d = 1, $lt = 2, $coada = 9
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.