行,所以我在学习PHP的准备语句,因为我听说他们是更安全了很多,MySQL的预处理语句功能
所以,现在即时通讯刚刚起步的功能手。我已经做了一个连接到MySQL数据库的函数,并将其放在一个不同的文件夹中,我想使用它。所以我想要在1个文件中的所有主要功能,然后我想从我需要它们的页面上的文件调用它们。但现在PHP不喜欢它。
这是我的functions.php页面
function mysqlconnect(){
$host = 'localhost';
$port = 3306; // This is the default port for MySQL
$database = '';
$username = '';
$password = '';
// Construct the DSN, or "Data Source Name". Really, it's just a fancy name
// for a string that says what type of server we're connecting to, and how
// to connect to it. As long as the above is filled out, this line is all
// you need :)
$dsn = "mysql:host=$host;port=$port;dbname=$database";
// Connect!
$db = new PDO($dsn, $username, $password);
}
这里是我的测试页面只是测试调用该函数。
include 'functions/functions.php';
mysqlconnect();
$_POST['fish'] = "shadow" ;
$statement = $db->prepare("SELECT * FROM users WHERE username = ?");
$statement->execute(array($_POST['fish']));
while ($result = $statement->fetchObject()) {
echo $result->username;
echo "<br />";
}
通知我包括文件和调用功能,但即时得到:
说明:未定义变量:分贝 致命错误:调用一个成员函数准备()在非objec
如果我把连接放在同一个php文件中,它一切正常。但是当然,像同一个文件中的所有函数一样,只需在需要时调用它们即可。我究竟做错了什么 ??
为什么你要手动设置'$ _POST'超全球? – PeeHaa