我想根据输入的输入
编写一个显示数据库元素的函数这是我的代码,但我在第34行出错了
注意:未定义的索引:table [1] [" des_dem"] in 第34行的C:\ xampp \ htdocs \ gpc \ test.php
<?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>
非常感谢您回答我的问题:)
答案 0 :(得分:0)
您必须使用global
关键字来执行此操作,您无法在$ GLOBALS数组中调用它,请执行此操作。
function afficher()
{
global $table;
echo $table[0][1];
}