PHP OOP 和 MySQL

标签 php mysql oop

我已经编写了一个程序版本的脚本,可以将 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);

?>

在浏览器中,我得到零输出,当我执行 a 时我什么也看不到

SELECT * FROM general_log;

在 MySQL 数据库上。

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

最佳答案

砰!!!

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

类文件

    <?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);
    }

?>

关于PHP OOP 和 MySQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17614779/

相关文章:

javascript - MeshLab Quadric Edge Collapse Decimation 在 JavaScript、PHP 或 Python 中保留纹理?

php - 轻量级、基于 PHP 的布局框架……知道吗?

php - MySQL查询根据投票加入2个表

java - 如何在 servlet 中获取所选单选按钮的值并将其存储在数据库中?

javascript - 在javascript中同一类的其他方法中获取数组

PHP OOP 使用 mysql 从数据库中获取数组

PHP count() 在非空数组上返回 NULL

php - 解析 PHP SDK 版本要求

php - MySQL将双空格 ""存储为问号 "?"

java - 编排多个 google API 客户端的最佳方法是什么?