1

I have written a procedural version of a script that will sync an iTunes Library with another drive, in my case a NAS. Chatting with some people at work and they suggested that it might be better, neater and a bit cooler to write in using objects. I like a challenge so I thought yeah I could give that a go. I have been reading around for the past couple of days, trying a few things without a lot of success. I have today been trying to follow the tutorials at; 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/.

While I find the principals of Classes, objects and methods / functions easy enough, their execution vexes me.

Here is the code I have written to perform a simple SQL SELECT query.

Below is my classFile

    <?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.
        }
    }


?>

Below is my PHP file called in the browser.

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

?>

In the browser I get zero output and I don't see anything when I do a

SELECT * FROM general_log;

On the MySQL DB.

Have a look at my in code comments to get an idea of where my head is at. If someone can explain this in simple terms, what's gone wrong and what I need to do to change it,it would really help me out.

5
  • If you are keen to use PHP with OOP get a MVC (Model View Controller), you will realise that your classes are actually models that come with a lot of built in DB functionality. Commented Jul 12, 2013 at 12:25
  • your code should print myClass Object ( ) are you getting this Commented Jul 12, 2013 at 12:28
  • 1
    @We0: If you're instructing someone to try MVC without first learning OOP, you don't know MVC. Commented Jul 12, 2013 at 12:32
  • @Simon, since you're learning you'd better learn PDO while you're at it. It's much better than mysqli_* Commented Jul 12, 2013 at 12:54
  • @MadaraUchiha His words "While I find the principals of Classes, objects and methods / functions easy enough", I learnt MVC and OOP at the same time... There is a lot of documentation on the combination of the two. Commented Jul 12, 2013 at 13:33

5 Answers 5

4

BOOM!!!

So I have managed to answer my own question and I thought I would share the solution I found with everyone.

Class file

    <?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 file...

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

?>
Sign up to request clarification or add additional context in comments.

Comments

1

yes there is mistake in your code. you have print the object of the class that's why displayed the nothing.

just follow the below code

//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);

1 Comment

Hi, thanks for the response but I have tried that and still nothing. I have also tried vardump($data);
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.
}
}
?>

Below is your PHP file called in the browser.

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'];  
}
?>

i run the same code, it work fine for me.

1 Comment

Hi, I have just tried this solution and still no success. Again I don't even see the SQL SELECT statement in the mysql logs. This is leading me to believe there is something wrong with either the connection or the execution of the query. I am going to try and use Wireshark to see what's going on. Thanks for the effort though.
0

You can't use

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

because you called your property dbCon

so

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

or just rename dbCon to mysqli

1 Comment

Hi, Yeah I had spotted that and had changed it after I posted. I was just trying to make it clearer what was my variable and what was the mysqli call. It was previously mysqli.
0

You're expecting data to be in $results but query() returns a result identifier/object not the result data, so you need to iterate a fetch function to get it, and return the rows not $results:

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

return $rows;

Also you shouldn't use the var syntax for properties, because that is old PHP4 style. Instead you should use the PHP5 style.

2 Comments

Hi, Thanks for the response. Alas still no success. I am looking at the general.log table of the MySQL DB and is not even showing a SELECT query being run against the staging table. I do not believe the PHP is even making a successful connection to the DB.
You need to output mysqli_connect_error() to see if there is a connection error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.