所以我正在尝试登记表格。我创建了一个php MySQL类,它使用构造函数为我的addElement函数建立与新的MySQLi()for verifyLogin函数和mysql_connect()的连接。
错误在于我的addElement函数,SQL语句正确写出但似乎没有连接到数据库。我已经检查过所有的名字都是正确的。有什么想法吗?
<?php
class MySQL {
private $connection;
private $conn;
private $databaseName;
function __construct($dbServer, $dbUser, $dbPassword, $dbName) {
$this->connection = new MySQLi($dbServer, $dbUser, $dbPassword, $dbName)
or die('PROBLEM CONNECTING TO DATABASE');
$this->conn = mysql_connect($dbServer, $dbUser, $dbPassword);
echo $this->conn;
$databaseName = $dbName;
}
function verifyLogin($table, $email, $password) {
$query = "SELECT *
FROM ?
WHERE email = ?
AND password = ?
LIMIT 1";
if($statement = $this->connection->prepare($query)) {
$statement->bind_param('sss', $table, $email, $password);
$statement->execute();
if($statement->fetch()) {
$statement->close();
return true;
}
else {
return false;
}
}
}
function addElement($table, $firstName, $lastName, $email, $mobile, $password,
$faculty, $campus) {
$query = "INSERT INTO $table (first_name, last_name, email, mobile, password, faculty, campus_location)
VALUES('$firstName', '$lastName','$email','$mobile',
'$password','$faculty','$campus');";
echo $query;
mysql_select_db($this->databaseName, $this->conn);
if(!mysql_query($query)) {
die('Error: ' . mysql_error());
}
mysql_close($this->connection);
}
}
?>
答案 0 :(得分:2)
要在MySQLi中选择数据库,您需要选择它:
$db = new mysqli("localhost", "my_user", "my_password", "database_name");
或
$db = new mysqli("localhost", "my_user", "my_password");
$db->select_db('database_name');
而不是MySQL:
$db = mysql_connect('localhost', 'my_user', 'my_password');
$db_selected = mysql_select_db('database_name', $db);
MySQLi:http://www.php.net/manual/en/mysqli.select-db.php
MySQL:http://www.php.net/manual/en/function.mysql-select-db.php
答案 1 :(得分:0)
你正在混合mysql和mysqli php驱动程序。
由于不推荐使用mysql驱动程序,我建议使用mysqli。
PHP文档: http://www.php.net/manual/en/book.mysqli.php
良好的概述让您入门: http://www.dreamincode.net/forums/topic/54239-introduction-to-mysqli-and-prepared-statements/