我编写了一个脚本的程序版本,它将 iTunes 库与另一个驱动器同步,在我的例子中是一个 NAS。在工作中与一些人聊天,他们建议使用对象编写可能会更好、更整洁、更酷。我喜欢挑战,所以我想是的,我可以试一试。在过去的几天里,我一直在阅读,尝试了一些事情但没有取得很大成功。我今天一直在尝试遵循以下教程; http://buildinternet.com/2009/07/an-introduction-to-object-oriented-php-part-1/ , http://buildinternet.com/2009/07/an-introduction-to-object-oriented -php-part-2/和http://buildinternet.com/2009/07/an-introduction-to-object-oriented-php-part-3/。
虽然我发现类、对象和方法/函数的原理很简单,但它们的执行让我很烦恼。
这是我为执行简单的 SQL SELECT 查询而编写的代码。
下面是我的类文件
<?php //classFile...
class myClass { // Part 2 of three part series
//Initiate class variables
public $server = 'localhost';
public $user = 'itunes';
public $passwd = 'itunes';
public $db = 'itunes_sync';
public $dbCon; //Variable to hold the mysqli connection function
function __construct(){
//$this->dbCon means reference the variable within this class called mysqli
$this->dbCon = mysqli($this->server, $this->user, $this->passwd, $this->db);
}
function getStaging(){
//Peform an SQL SELECT Query
$myQuery = "SELECT * FROM staging";
/*
*Define new function variable $resuls
*$results = $mysqli class variable
*$mysql class variable has been assigned the function mysqli which has an internal function called query.
*The second query is not the function variable named above. The query function is passed the $query
*varibale as its input, in this case the SQL SELECT...
*/
$results = $this->mysqli->query($myQuery);
$rows = array();
while($row = $results->fetch_assoc()){
$row[] = $row;
}
return $results; //This function returns the results.
}
}
?>
下面是我在浏览器中调用的 PHP 文件。
<?php
//Include class file in index.php file
require_once('myClass.class.php');
//Initiate a new object of myClass() in variable $myClassObj
$myClassObj = new myClass();
$data = $myClassObj->getStaging();
print_r($data);
?>
在浏览器中我得到零输出,当我做一个时我什么也看不到
SELECT * FROM general_log;
在 MySQL 数据库上。
看看我的代码注释,了解我的想法。如果有人可以简单地解释这一点,出了什么问题以及我需要做些什么来改变它,那真的会帮助我。