【问题标题】:trying to call a variable inside a function in php试图在php中调用函数内的变量
【发布时间】: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


【解决方案1】:

您必须使用 global 关键字来执行此操作,您不能像那样在 $GLOBALS 数组中调用它,请执行此操作..

function afficher()
 {
     global $table;
     echo $table[0][1];
 }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-07-14
    • 2011-12-18
    • 2016-08-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多