此代码从数据库中获取2个项目的数组。我想根据该数组设置$_GET
变量值。
<?php
$servername = "localhost";
$username = "username";
$password = "password";
// create connection
$conn = new mysqli($servername, $username, $password);
// check connection
if ($conn->connect_error){
die("connection failed: " . $conn->connect_error);
}
// connect to DB
$db_selected = mysqli_select_db($conn, 'article');
if (!$db_selected){
die("can't use article : " .mysqli_error());
}
// extract databases table to PHP array
$query = "SELECT * FROM `articles`";
$result = $conn->query($query);
$article_array = array();
while($row = $result->fetch_array(MYSQLI_ASSOC)){
$artic = $row['name'];
$amount = $row['quantity'];
$article_array[$artic] = $amount;
}
// test array
//print_r($article_array); ---> works!
// print the array in the pos of id
if(isset($_GET['id']){
echo 'id is: '.$article_array[$_GET['id'];
} else {
echo 'soemthing is wrong';
}
?>
上面是我尝试的内容:网址localhost/hello.php?id=1
应检索变量ID并将其设置在$_GET
变量中。但是,最后一个echo语句不能按预期工作。
答案 0 :(得分:0)
试试这个例子:
链接到:page.php?color = 2
page.php文件
$colours = array();
$colours[] = 'Red';
$colours[] = 'White';
$colours[] = 'Blue';
if(isset($_GET['color']){
echo 'Selected colour is: '.$colours[$_GET['color'];
} else {
echo 'Something is wrong';
}
应输出&#34;所选颜色为蓝色&#34 ;;