我知道问题在于mysqli_query的第一个参数,但我不知道我的生活中找出了确切的参数应该是什么。我可以使用mysql_query,但我正在尝试学习OOP并转换为mysqli。我希望能够在需要时调用connect(并最终断开连接),然后在连接后执行查询。任何帮助都会很棒。
database.php中
class Database{
private $db_host = "localhost";
private $db_user = "root";
private $db_pass = "root";
private $db_name = "db";
public function __construct(){
$this->connect();
}
public function connect()
{
//If database is not connected, look to connect
if(!$this->con)
{
$myconn = mysqli_connect($this->db_host,$this->db_user,$this->db_pass);
//If database is connected, select table
if($myconn)
{
$seldb = mysqli_select_db($this->db_name,$myconn);
//If database is selected, return conn
if($seldb)
{
$this->con = true;
return true;
} else
{
return false;
}
} else
{
return false;
}
} else
{
return true;
}
}
public function disconnect()
{
if($this->con)
{
if(mysqli_close())
{
$this->con = false;
return true;
}
else
{
return false;
}
}
}
}
school.php
require_once('database.php');
class Schools{
public function __construct()
{
$connection = new Database();
$this->getAllSchools();
}
public function getAllSchools($connection){
$schoolsql = "SELECT SID, schoolName, schoolCity, schoolCountry, schoolRegion FROM schools ORDER BY SID";
$schoolquery = mysqli_query($connection->con, $schoolsql);
$results = array();
while($row = mysqli_fetch_array($schoolquery))
{
$results[] = array(
'id' => $row['SID'],
'name' => $row['schoolName'],
'city' => $row['schoolCity'],
'country' => $row['schoolCountry'],
'region' => $row['schoolRegion']
);
}
$json = json_encode($results);
echo $json;
}
}
$school = new Schools;
我知道这就是失败的地方。 $ schoolquery = mysqli_query($ connection-> con,$ schoolsql);
我只是不知道mysqli连接变量对数据库类的期望是什么?
这组代码“有效”。我知道它们是安全问题,但是现在我正在学习我只是想从数据库中获得我期望的结果 - 但最终代码结构并不是我想要的。我更喜欢让database.php文件包含一个我可以在schools.php文件中实例化的类。现在它只是创建连接,我可以将其加载到Schools类中。我已经在SO(以及Google)上查看了几个mysqli类连接问题,并尝试了几种排列,但我无法使Database类选项工作。
database.php中
$db_host = "localhost";
$db_user = "root";
$db_pass = "root";
$db_name = "db";
$mysqli = new mysqli($db_host, $db_user, $db_pass, $db_name);
if (!$mysqli) {
die("Connection failed: " . mysqli_connect_error());
}
schools.php
class Schools{
function __construct($mysqli){
$this->mysqli = $mysqli;
}
//Method to return all schools
function getAllSchools () {
$query = "SELECT SID, schoolName, schoolCity, schoolCountry, schoolRegion FROM schools ORDER BY SID ASC";
$results = array();
if ($result = $this->mysqli->query($query) )
{
while ($row = $result->fetch_assoc()) {
$results[] = array(
'id' => $row['SID'],
'name' => $row['schoolName'],
'city' => $row['schoolCity'],
'country' => $row['schoolCountry'],
'region' => $row['schoolRegion']
);
}
$json = json_encode($results);
echo $json;
$result->close();
} else {
echo "No results";
}
}
//Method to return one school
function getASchool ($id) {
$query = "SELECT SID, schoolName, schoolCity, schoolCountry, schoolRegion FROM schools WHERE SID=$id";
$results = array();
if ($result = $this->mysqli->query($query) )
{
while ($row = $result->fetch_assoc()) {
$results[] = array(
'id' => $row['SID'],
'name' => $row['schoolName'],
'city' => $row['schoolCity'],
'country' => $row['schoolCountry'],
'region' => $row['schoolRegion']
);
}
$json = json_encode($results);
echo $json;
$result->close();
} else {
echo "No results";
}
}
}
$school = new Schools($mysqli);
$school->getASchool("120");
答案 0 :(得分:0)
我更喜欢让database.php文件包含一个类
你拥有它。请注意,Mysqli已经是一流的课程了。
我可以在schools.php文件中实例化。
你不能拥有它。实例化mysqli类意味着创建一个到mysql的新连接,你肯定只希望每个脚本一次。因此,您只需要将其实例化一次,然后将此唯一实例传递给其他类,就像您一样。
答案 1 :(得分:-1)
注意: 关于代码结构有几点可以/应该改进,但是这个答案只是为了回答你的直接问题。
有两部分:
在class Database
,而不是return true;
,您需要return $this->con
,如下所示:
class Database{
private $db_host = "localhost";
private $db_user = "root";
private $db_pass = "root";
private $db_name = "db";
// Add this class variable to store con
private $con = FALSE;
public function __construct(){
// Return the connection
return $this->connect();
}
public function connect() {
//If database is not connected, look to connect
if( ! $this->con) {
$myconn = mysqli_connect($this->db_host,$this->db_user,$this->db_pass);
//If database is connected, select table
if($myconn) {
$seldb = mysqli_select_db($this->db_name,$myconn);
//If database is selected, return conn
if($seldb) {
// Assign the class variable to the connection
$this->con = $myconn;
// Return the connection
return $this->con;
} else {
return false;
}
} else {
return false;
}
} else {
// Return the connection
return $this->con;
}
}
// ... the rest of your class ...
}
然后,在class Schools
中,您应该为$connection
使用类变量,如下所示:
class Schools{
// Class variable to store your database connection
private $connection;
public function __construct() {
// Assign the connection to your class variable
$this->connection = new Database();
$this->getAllSchools();
}
public function getAllSchools($connection){
$schoolsql = "SELECT SID, schoolName, schoolCity, schoolCountry, schoolRegion FROM schools ORDER BY SID";
// Use the class variable to connect
$schoolquery = mysqli_query($this->connection, $schoolsql);
$results = array();
// ... rest of your code
}
}
根据扩展答案修改:
首先,你的路线在这里:
$school = new Schools($mysqli);
- 这是dependency injection的一种形式。这是一个很好的学习/熟悉的东西,因为它在编写那些依赖"依赖的课程时非常有价值。在其他课程上。
为了理清下面代码为什么没有按照您的期望进行操作,我需要查看您的代码版本。请注意,我提供的Database
类修订版有三行新return
行。如果缺少任何一个,代码可能无法正常工作。
代码实际上应该失败并发生致命错误 - 函数getAllSchools
需要一个参数,但对它的调用不包括该参数。
最后,最好的办法是学习如何调试它。通过修改Schools
类功能,进行一些故障排除,如下所示:
public function getAllSchools(){
$schoolsql = "SELECT SID, schoolName, schoolCity, schoolCountry, schoolRegion FROM schools ORDER BY SID";
// Use the class variable to connect
$schoolquery = mysqli_query($this->connection, $schoolsql);
// Is there an issue? Let's output it:
var_dump(mysqli_error($this->connection));
$results = array();
// ... rest of your code
}
向我们提供错误的详细信息,并确保使用您生成错误的特定代码更新我们。