Click to See Complete Forum and Search --> : Oop


sandro27
09-17-2005, 02:49 PM
I trying to get into this OOP thing and I had my first attempt as follows:

class db {

function connect($db_host, $db_user, $db_pass, $db_name) {

mysql_connect($db_host, $db_user, $db_pass);

mysql_select_db("$db_name") or die(mysql_error());

}

function query($query) {

$query = mysql_query($query) or die(mysql_error());

return $query;

}
function fetch_array($query) {
$query = mysql_fetch_array($query);

return $query;

}
function fetch_row($query) {

$query = mysql_fetch_row($query);

return $query;

}
function close() {
mysql_close();

}
}

?>

<?
$db->connect("localhost", "user", "pssword", "db");
$query = $db->query("select * from blog_images");
$move="file directory/";
?>

Unfortunately I get the error:

Fatal error: Call to a member function on a non-object in /home/floripai/public_html/weblog/intro.php on line 75 ($db->connect("localhost", "user", "pssword", "db");)

I might have been trying the wrong approach with the right class.

Does anyone have any advice ?

Thanks

theuedimaster
09-17-2005, 03:36 PM
Well I've done OOP in java, and don't you have to initialize an object of the db class? Lke db $test = new db;

Stephen Philbin
09-17-2005, 10:02 PM
Well yes and no. The error is being caused by a method of an uninstantiated object being called, but only because it is being asked for as if it has been instantiated already.

Instead of using $db->connect("localhost", "user", "pssword", "db");. You could use the scope resolution operator "::" and point to the method in the uninstantiated class directly. To do that you just use db::connect("localhost", "user", "pssword", "db");

Using "->" indicates you want to use the member or method of a class that has already been instantiated into an object. To create an instance of an object from a class you use $my_db_object_name = new db();

It is important to understand the difference between a class and an object. It is a fundamental key to the whole thing. Think of a class as like the plans for a building, rather than the building its self.You only get a building by $building_a = new building(); based on the plans.

SpectreReturns
09-17-2005, 10:37 PM
To access the current instance of a class, use $this.

EDIT: Sorry, read that wrong. You need to initialize the variable as a class before you can send it actions.

sandro27
09-23-2005, 09:25 AM
Thank you Guys for the useful tips.

The result came as this
$db= new db;
$db->connect("$db_host", "$db_user", "$db_pass", "$db_name");
$db->query("select * from sd_name");
$list= $db->fetch_array($query);


However I got a function internal error :Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/floripai/public_html/weblog/intro.php on line 20


I have learned it fromPHP freaks (http://www.phpfreaks.com/quickcode/Simple_MySQL_Database_Querying_Class/31.php) , I hope it is ok.

sandro27
09-23-2005, 09:27 AM
Special thanks for stephen who did explain me clearly about Objects and classes :D

Stephen Philbin
09-23-2005, 06:04 PM
Happy to help. ;) So does it all work now or do you still get that internal error?

SpectreReturns
09-23-2005, 06:33 PM
Your syntax is a bit wonky...

sandro27
09-23-2005, 09:04 PM
Wonky ? well I have learned it from a PHP freaks article. Do you have a better suggestion ?

SpectreReturns
09-23-2005, 09:33 PM
$db= new db;
$db->connect($db_host, $db_user, $db_pass, $db_name);
$db->query("SELECT * FROM sd_name");
$list= $db->fetch_array($query);

ShrineDesigns
09-24-2005, 01:11 PM
this is what i use, constants.php<?php
define('SQL_ASSOC', 1);
define('SQL_NUM', 2);
define('SQL_BOTH', 3);
?>mysql4.php<?php
class sql_layer
{
var $id; // connection id

function affected()
{
return @mysql_affected_rows($this->id);
}
function close()
{
return (SQL_PERSISTENT) ? true : @mysql_close($this->id);
}
function error()
{
return @mysql_error($this->id);
}
function fetch(&$result, $result_type = SQL_BOTH)
{
return @mysql_fetch_array($result, $result_type);
}
function found()
{
$result = @mysql_query("SELECT FOUND_ROWS()", $this->id);
return @mysql_result($result, 0);
}
function free(&$result)
{
return @mysql_free_result($result);
}
function num_cols($result)
{
return @mysql_num_fields($result);
}
function num_rows($result)
{
return @mysql_num_rows($result);
}
function query($query)
{
return @mysql_query($query, $this->id);
}
function result($result, $row = 0, $field = NULL)
{
return @mysql_result($result, $row, $field);
}
function sql_layer()
{
$this->id = (SQL_PERSISTENT) ? @mysql_pconnect(SQL_SERVER, SQL_USERNAME, SQL_PASSWORD) : @mysql_connect(SQL_SERVER, SQL_USERNAME, SQL_PASSWORD);
return ($this->id !== false && @mysql_select_db(SQL_DATABASE, $this->id));
}
}
?>mysqli.php<?php
class sql_layer
{
var $id; // connection id

function affected()
{
return @mysqli_affected_rows($this->id);
}
function close()
{
return (SQL_PERSISTENT) ? true : @mysqli_close($this->id);
}
function error()
{
return @mysqli_error($this->id);
}
function fetch(&$result, $type = SQL_BOTH)
{
$fetch = @mysqli_fetch_array(&$result, $type);
return (empty($fetch)) ? false : $fetch;
}
function found()
{
$result = @mysqli_query($this->id, "SELECT FOUND_ROWS()");
return $this->result($result, 0);
}
function free(&$result)
{
return @mysqli_free_result($result);
}
function num_cols($result)
{
return @mysqli_num_fields($result);
}
function num_rows($result)
{
return @mysqli_num_rows($result);
}
function query($query)
{
return @mysqli_query($this->id, $query);
}
function result($result, $row = 0, $field = NULL)
{
@mysqli_data_seek($result, $row);
$field = (is_null($field)) ? 0 : $field;
$set = @mysqli_fetch_array($result, SQL_BOTH);
return (!empty($set)) ? $set[$field] : false;
}
function sql_layer()
{
$this->id = (SQL_PERSISTENT) ? @mysqli_pconnect(SQL_SERVER, SQL_USERNAME, SQL_PASSWORD, SQL_DATABASE) : @mysqli_connect(SQL_SERVER, SQL_USERNAME, SQL_PASSWORD, SQL_DATABASE);
return ($this->id !== false);
}
}
?>config.php<?php
define('SQL_PLATFORM', 'mysql4');
define('SQL_SERVER', 'localhost');
define('SQL_USERNAME', 'root');
define('SQL_PASSWORD', '');
define('SQL_DATABASE', 'test');
define('SQL_PERSISTENT', 0);
?>database.php<?php
switch(SQL_PLATFORM)
{
case 'mysql3':
include($root . 'db/mysql3.php');
break;
case 'mysql4':
include($root . 'db/mysql4.php');
break;
case 'mysqli':
include($root . 'db/mysqli.php');
break;
case 'pgsql':
include($root . 'db/pgsql.php');
break;
case 'sqlite':
include($root . 'db/sqlite.php');
break;
}
$db = new sql_layer;

if(!$db || $db->id === false)
{
die('unable to connect to database');
}
?>

sandro27
09-24-2005, 06:11 PM
Wow !

This looks pretty good ( and ambitious) I will have a look at it and let you know the results.

Database connections is something that seems to have many different approaches....

Thanks