我一直在学习如何创建会话对象的一些课程,这很好,如果我把完整的代码放到PHP文件上,一切都很棒!
我想做的是将它放在另一个模块(PHP文件)中,只需使用一行(或等效的)来执行此操作,例如GetSessiondata();
<?PHP
$sqlCon = new DBConnect("Databaselocation","Database","Usr","Pss");
$UserDataSet = $sqlCon->GetUserList("SELECT * FROM Users");
echo "<br /><br />";
echo "<br /><br />";
if ($UserDataSet)
{
echo "<table>" . "<thead>" ;
echo "<tr><th scope=\"col\">" . 'Usr' . "</th>";
echo "<th scope=\"col\">" . 'Lvl' . "</th></tr></thead><tbody>";
foreach($UserDataSet as $data)
{
echo "<td>" .$data->GetUsrName()."</td>" ;
echo "<td>" .$data->GetUsrLevel()."</td></tr>" ;
}
echo "<tfoot><tr><th scope=\"row\" colspan=\"2\">" . 'Total Users = 2' . "</th></tr></tfoot>";
echo "</tbody>" . "</table>" ;
}
else
echo "nothing Found in DB!";
?>
解决方法:
您需要“需要”您要使用它的文件.
这是一个例子
Working with classes:
Whatever.PHP
class Whatever {
public function __construct() {
// Ever when the code is instantiated, this will be called too!
}
public function myMethod() {
echo 'hello';
}
}
的index.PHP
require_once('./Whatever.PHP');
$whatever = new Whatever();
$whatever->myMethod();
Without classes:
的functions.PHP:
function whatever(){ echo 'hello'; }
require_once('./functions.PHP');
whatever();
阅读更多:
要求:http://php.net/manual/es/function.require.php
Require_once:http://php.net/manual/es/function.require-once.php