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.