我目前有四个页面一起工作:
index.php
dbconfig.php
databass.class.php
View_Item.php
在databass.class.php
内,我在$API_Key = XXXXXXX;
位于页面顶部,在页面的下方,include
到View_Item.php
。
查看index.php
,显示回显的HTML内容,但未提取其他内容,因此我在该页面添加了测试:
echo "<h1>\$APK_Key =".$API_Key."</h1>";
这不会返回任何内容,只显示&#34; $ APK_Key =&#34;
如果包含另一个PHP页面,为什么$APK_Key
变量在提取的内容中不起作用?
请注意:这不是寻求调试帮助的问题 - 我对PHP编程有点新,想知道如何,而不是为什么我的版本不工作。我提出这一点在这一点上看到用户投票决定关闭我的问题。
更新
index.php
databass.class.php
databass.class.php
dbconfig.php
$API_Key
View_Item.php
**更新2:
databass.class.php
读到:
<?php
require("dbconfig.php");
$API_Key = XXXXXXXX;
class MYCLASS {
private $conn;
public function __construct() {
$database = new Database();
$db = $database->dbConnection();
$this->conn = $db;
}
public function runQuery($sql) {
$stmt = $this->conn->prepare($sql);
return $stmt;
}
public function prepare($sqls) {
$stmt = $this->conn->prepare($sqls);
return $stmt;
}
public function itemTypes($type) {
try {
$stmt = $this->conn->prepare("SELECT type FROM items WHERE type=:type");
$stmt->execute(array(':type'=>$type));
$userRow=$stmt->fetch(PDO::FETCH_ASSOC);
if($stmt->rowCount() > 0) {
return $stmt->rowCount();
}
}
catch(PDOException $e) {
echo $e->getMessage();
}
}
public function listItem($type,$name) {
if ($name) {
try {
$stmt = $this->conn->prepare("SELECT id, name, buy_Price, sell_Price FROM items WHERE type=:type AND name=:name");
$stmt->execute(array(':type'=>$type, ':name'=>$name));
$userRow=$stmt->fetch(PDO::FETCH_ASSOC);
echo "<p>
<span><div class=\"items\"><div class=\"items-lg items-lg-".$userRow['id']."\"></div></div></span><br>
<span>".$userRow['name']."</span><br>
<span>".$userRow['buy_Price']."</span><br>
<span>".$userRow['sell_Price']."</span>
</p><br><br><br><br>";
}
catch(PDOException $e) {
echo $e->getMessage();
}
include "../Assets/PHP/Pages/Items/View_Item.php";
} else {
try {
$stmt = $this->conn->prepare("SELECT id, name, buy_Price, sell_Price FROM items WHERE type=:type");
$stmt->execute(array(':type'=>$type));
$userRow=$stmt->fetchall(PDO::FETCH_ASSOC);
echo "<table>
<thead>
<tr>
<th>IMG</th>
<th>Item</th>
<th>Buy Price</th>
<th>Sell Price</th>
</tr>
</thead>
<tbody>";
foreach($userRow as $result => $value){
echo "\n <tr>
<td><div class=\"items\"><div class=\"items-lg items-lg-".$value['id']."\"></div></div></td>
<td>".$value['name']."</td>
<td>".$value['buy_Price']."</td>
<td>".$value['sell_Price']."</td>
</tr>";
}
echo "\n </tbody>\n</table>";
}
catch(PDOException $e) {
echo $e->getMessage();
}
}
}
}
error_reporting(-1); // reports all errors
ini_set("display_errors", "1"); // shows all errors
ini_set("log_errors", 1);
?>
答案 0 :(得分:0)
您需要在使用变量之前声明变量。如果index.php看起来像这样:
echo "<h1>\$API_Key =".$API_Key."</h1>";
include 'databass.class.php'; //where $API_Key is defined
您在定义之前尝试使用$API_Key
(第一行)(在第二行的文件中)。
首先放置include
行。