Simon
Simon

Reputation: 238

PHP OOP & MySQL

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.

Upvotes: 1

Views: 12954

Answers (5)

Nikhil sHETH
Nikhil sHETH

Reputation: 530

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

Upvotes: 1

Simon
Simon

Reputation: 238

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

?>

Upvotes: 4

MrCode
MrCode

Reputation: 64526

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.

Upvotes: 0

ashish patel
ashish patel

Reputation: 34

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

Upvotes: 1

Karo
Karo

Reputation: 744

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

Upvotes: 0

Related Questions