1

我编写了一个脚本的程序版本,它将 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 数据库上。

看看我的代码注释,了解我的想法。如果有人可以简单地解释这一点,出了什么问题以及我需要做些什么来改变它,那真的会帮助我。

4

5 回答 5

4

繁荣!!!

所以我设法回答了我自己的问题,我想我会与大家分享我找到的解决方案。

类文件

    <?php

        class db {
                public $server = 'localhost';
                public $user = 'itunes';
                public $passwd = 'itunes';
                public $db = 'itunes_sync';
                public $dbCon;

        function __construct(){
                $this->dbCon = mysqli_connect($this->server, $this->user, $this->passwd, $this->db);
        }

          function __destruct(){
                mysqli_close($this->dbCon);
        }

        function select(){
                $myQuery = "SELECT * FROM staging;";
                $results = mysqli_query($this->dbCon, $myQuery);
                return $results;
        }

    }

?>

PHP 文件...

<?php

    require_once('class.php');

    $myClassObj = new db();

    //$myClassObj->db();
    $data = $myClassObj->select();

    $selectArray = array();

    while($row = mysqli_fetch_assoc($data)){
        $selectArray[] = $row;
        print_r($row);
    }

?>
于 2013-07-12T21:33:15.973 回答
1
<?php //classFile...
class myClass { // Part 2 of three part series

//Initiate class variables
var $server = 'localhost';
var $user = 'itunes';
var $passwd = 'itunes';
var $db = 'itunes_sync';

var $dbCon; //Variable to hold the mysqli connection function

function __construct(){

    //$this->dbCon means reference the variable within this class called mysqli
    $this->dbCon = new MySQLi($this->server, $this->user, $this->passwd, $this->db);
}

function getStaging(){
    //Peform an SQL SELECT Query
    $myQuery = "SELECT * FROM registration";

    /*
     *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 = mysqli_query($this->dbCon ,$myQuery);

    return $results; //This function returns the results.
}
}
?>

下面是您在浏览器中调用的 PHP 文件。

require_once("myClass.class.php");
$myClassObj = new myClass();

$data = $myClassObj->getStaging();
while($f1 = mysqli_fetch_array($data))
{
echo "<br>".$f1['id']."  ".$f1['email']."  ".$f1['pword'];  
}
?>

我运行相同的代码,它对我来说很好。

于 2013-07-12T12:49:16.653 回答
1

是的,您的代码中有错误。你已经打印了类的对象,这就是为什么显示什么都没有。

只需按照以下代码

//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);
于 2013-07-12T12:23:46.047 回答
0

您期望数据在$resultsquery()返回结果标识符/对象而不是结果数据,因此您需要迭代 fetch 函数来获取它,并返回行 not $results

$results = $this->dbCon->query($myQuery);
$rows = array();
while($row = $results->fetch_assoc())
{
    $rows[] = $row;
}

return $rows;

你也不应该使用var属性的语法,因为那是旧的 PHP4 风格。相反,您应该使用PHP5 样式

于 2013-07-12T12:33:22.383 回答
0

你不能使用

$this->mysqli->query($myQuery);

因为你打电话给你的财产dbCon

所以

$results = $this->dbCon->query($myQuery);

或者只是重命名dbConmysqli

于 2013-07-12T12:22:53.207 回答