編輯:討論here click here後,我決定使用弗朗西斯阿維拉的解決方案,它更安全,而且我沒有找到一個方法來利用我的路,只有大家都說它的安全風險使用eval( )。如果您找到了方法,請利用我的方式,請發表評論。 :-),以便我瞭解它以及另一個使用eval的用戶:-)創建動態數量的變量
還有一件事情不使用eval(),它比我想要的更慢。
編輯:
一個積極的事情,也就是在弗朗西斯阿維拉的解決辦法是,你只需要特定的只有2瓦爾,$ SQL + $ INPUT,無需更多的弟弟$輸出。
編輯:刪除舊的問題,並用它替換它,所以你可能有一個更好的想法,我想要做什麼。
我發現一個解決方案如何製作一個動態數量的變量,但它不是最優解決方案。因爲我需要編輯/添加更多的線,如果變量的數量變得比8更高的那麼有沒有一種方法,使在開關功能上不要繼電器,其動態
public function readDB($readdb, $input, $output1) {
$sql = $readdb;
$stmt = $this->mysqli->prepare($sql);
if(!empty($input) && is_array($input)) {
$inputn = count($input);
switch($inputn) {
case "1":
$stmt->bind_param('s', $input[0][0]);
break;
case "2":
$stmt->bind_param('ss', $input[0][0], $input[1][0]);
break;
case "3":
$stmt->bind_param('sss', $input[0][0], $input[1][0], $input[3][0]);
break;
case "4":
$stmt->bind_param('ssss', $input[0][0], $input[1][0], $input[3][0], $input[4][0]);
break;
case "5":
$stmt->bind_param('sssss', $input[0][0], $input[1][0], $input[3][0], $input[4][0], $input[5][0]);
break;
case "6":
$stmt->bind_param('ssssss', $input[0][0], $input[1][0], $input[3][0], $input[4][0], $input[5][0], $input[6][0]);
break;
case "7":
$stmt->bind_param('sssssss', $input[0][0], $input[1][0], $input[3][0], $input[4][0], $input[5][0], $input[6][0], $input[7][0]);
break;
case "8":
$stmt->bind_param('ssssssss', $input[0][0], $input[1][0], $input[3][0], $input[4][0], $input[5][0], $input[6][0], $input[7][0], $input[8][0]);
break;
default:
break;
}
}
if (!$stmt) {throw new Exception($mysqli->error);}
$stmt->execute();
$stmt->store_result();
$checker = $stmt->num_rows;
if($checker !== 0) {
if(!empty($output1)) {
switch($output1) {
case "1":
$stmt->bind_result($output[0]);
while ($stmt->fetch()) {
$results[] = array($output[0]);
}
break;
case "2":
$stmt->bind_result($output[0], $output[1]);
while ($stmt->fetch()) {
$results[] = array($output[0], $output[1]);
}
break;
case "3":
$stmt->bind_result($output[0], $output[1], $output[2]);
while ($stmt->fetch()) {
$results[] = array($output[0], $output[1], $output[2]);
}
break;
case "4":
$stmt->bind_result($output[0], $output[1], $output[2], $output[3]);
while ($stmt->fetch()) {
$results[] = array($output[0], $output[1], $output[2], $output[3]);
}
break;
case "5":
$stmt->bind_result($output[0], $output[1], $output[2], $output[3], $output[4]);
while ($stmt->fetch()) {
$results[] = array($output[0], $output[1], $output[2], $output[3], $output[4]);
}
break;
case "6":
$stmt->bind_result($output[0], $output[1], $output[2], $output[3], $output[4], $output[5]);
while ($stmt->fetch()) {
$results[] = array($output[0], $output[1], $output[2], $output[3], $output[4], $output[5]);
}
break;
case "7":
$stmt->bind_result($output[0], $output[1], $output[2], $output[3], $output[4], $output[5], $output[6]);
while ($stmt->fetch()) {
$results[] = array($output[0], $output[1], $output[2], $output[3], $output[4], $output[5], $output[6]);
}
break;
case "8":
$stmt->bind_result($output[0], $output[1], $output[2], $output[3], $output[4], $output[5], $output[6], $output[7]);
while ($stmt->fetch()) {
$results[] = array($output[0], $output[1], $output[2], $output[3], $output[4], $output[5], $output[6], $output[7]);
}
break;
default:
echo "HERE";
break;
}
}
} else {
$results = "NO RESULTS";
}
$stmt->fetch();
$stmt->close();
$this->checker = $checker;
$this->results = $results;
$this->result = array('num_rows' => $checker, $results);
return $this->results;
我不明白你的問題,真的。但你可以用另一個變量值來創建變量,如下所示:{$ var} = true – Zar 2011-12-24 19:42:54
嗨,我更新了文本,如果你有時間可以看看它,也許你現在更好地理解它 – user1015314 2011-12-24 21:48:50