【发布时间】:2016-04-12 07:20:30
【问题描述】:
我想编写一个函数,根据我输入的输入显示我的数据库元素
这是我的代码,但第 34 行出现错误
注意:未定义索引:table[1]["des_dem"] in C:\xampp\htdocs\gpc\test.php 第 34 行
<?php
$element = $_GET["element"];
echo $element;
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "gpc";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// $sql = "SELECT * FROM bons WHERE des_dem='$element'" ;
$sql = "SELECT * FROM bons" ;
$result = $conn->query($sql);
if ($result->num_rows > 0) {
$table = $result->fetch_all(MYSQLI_ASSOC);
} else {
echo "0 results";
}/*
echo $table[1]["des_dem"];
echo "<br>";
echo $table[1]["des_dec"]; */
function afficher()
{
echo $GLOBALS['table[1]["des_dem"]'];
}
echo $table[4]["des_dec"];
echo "<br>";
afficher();
$conn->close(); ?>
<html>
<body>
<br>
<form action="test.php" method="get">
<tr>
<td>Name</td>
<td><input type="text" name="element"></td>
</tr>
<input type="submit" value="Submit">
</form>
</body>
</html>
非常感谢您回答我的问题:)
【问题讨论】:
-
这应该是:
echo $GLOBALS[$table[1]["des_dem"]];
标签: php database global-variables