LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-19-2009, 04:59 AM   #1
eco
Member
 
Registered: May 2006
Location: BE
Distribution: Debian/Gentoo
Posts: 412

Rep: Reputation: 48
Post PHP OOP moving arrays around functions?


Hi guys,

I'm trying to get the hang of PHP OOP and am at a loss on what to do next.

I created two classes
Db.php
Tables.php

The idea is that I send an SQL query to the Db class which connects to the database and returns the result in an array.

I then need to send the array to the Tables class to print out the results in an html table.

Problem is, I can't seem to get any data out of $data. How would you start writing the Tables class to extract data from the array?

Can this be done?

Here is a sample of what I did:

index.php
Code:
<?php

require_once 'classes/Db.php';
require_once 'classes/Tables.php';

$q1 = new Db;
$data = $q1->ReturnSqlQuery("SELECT User, Host FROM user;"
                                                        );
$t1 = new Tables;
$t1->UserHost($data);
Db.php
Code:
<?php

Class Db
{
        private $_host = "localhost";
        private $_user = "testuser";
        private $_pass = "test";
        private $_db   = "mysql";

        // Will need a new class Error to handle all errors

        private function ConnectToDatabase()
        {
                $link = mysql_connect($this->_host, $this->_user, $this->_pass);
                if (!$link) {
                die('Could not connect to the server: ' . mysql_error());
                }

                $db_selected = mysql_select_db($this->_db, $link);
                if (!$db_selected) {
                        die ('Could not connect to the database"' . $this->_db ." get error: " . mysql_error());
                }
        }

        private function DisconnectFromDatabase()
        {
                mysql_close($link);
        }

        public function ReturnSqlQuery($sql)
        {
                self::ConnectToDatabase();
                $result = mysql_query($sql);

                if (!$result) {
                        $message  = 'Invalid query: ' . mysql_error() . "\n";
                        $message .= 'Whole query: ' . $query;
                        die($message);
                }

                // return the result to a function that will build the right table.
                return $result;

        //      self::DisconnectFromDatabase();
        }
}
 
Old 01-19-2009, 06:57 AM   #2
eco
Member
 
Registered: May 2006
Location: BE
Distribution: Debian/Gentoo
Posts: 412

Original Poster
Rep: Reputation: 48
Quote:
$result = mysql_query($sql);
return $result;
I think I see where I am getting things wrong. I am assuming that the above returns an array but it seems the only way to extract the data from within $result is through the following... so what is in $result?

Code:
while ($row = mysql_fetch_assoc($result)){
    echo "$row[User] - $row[Host]<br>";
}
So can I restrict the handling of database and database data to the Db class or do I have to have the equivalent of the above to extract the SQL result into my tables?

Am I doing this all wrong?
 
Old 01-19-2009, 07:53 AM   #3
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,453

Rep: Reputation: 446Reputation: 446Reputation: 446Reputation: 446Reputation: 446
Hi

mysql_query returns a resource which is not an array. Why not just fetch all the rows and return an array instead? That way the database stuff will be in the Db class and not other places.

Instead of
PHP Code:
return $result
PHP Code:
$rows = array();
while (
$row mysql_fetch_assoc($result))
  
$rows[] = $row;
mysql_free_result($result);
return 
$rows
 
Old 01-19-2009, 08:07 AM   #4
eco
Member
 
Registered: May 2006
Location: BE
Distribution: Debian/Gentoo
Posts: 412

Original Poster
Rep: Reputation: 48
Precisely what I was after.
Thank you Guttorm
 
  


Reply

Tags
php



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP5 - OOP heritance and static functions and self:: emi_ramo Programming 5 07-09-2008 04:28 AM
PHP: why OOP over normal functions? konqi Programming 15 05-10-2008 08:10 PM
OOP in PHP patpawlowski Programming 5 11-20-2005 04:37 PM
Arrays of Functions and Classes problem. (C++) -=zAe=- Programming 2 02-24-2005 10:21 AM
Problem in C: arrays, functions and strings OrganicX Programming 15 03-18-2003 09:31 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:28 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration