...

View Full Version : Using Class to connect to a database



flint0131
11-22-2010, 11:29 AM
I have a problem using Class in PHP, this is my first time using OOP and I'm quit confused.

this is the code :


class db {
private $var_db_username, $var_db_password, $var_db_host;
private $var_db_used;
private $var_db_link;
static public function db_con($host, $username, $password) {
$this->var_db_username = $username;
$this->var_db_password = $password;
$this->var_db_host = $host;
/*$this->var_db_link = */mysql_connect($this->var_db_host, $this->var_db_username, $this->var_db_password) or
die("Error : ". mysql_error());
}

static public function db_select($dbname) {
$this->var_db_used = $dbname;
mysql_select_db($this -> var_db_used, $this -> var_db_link) or die("Error : ". mysql_error());
}
}

$obj = new db();
$obj->db_con("localhost", "root", "");
$obj->db_select("sample");


The problem is, it generates this error :

Fatal error: Using $this when not in object context in C:\xampp\htdocs\please\db.php on line 11

abduraooft
11-22-2010, 11:51 AM
That's not the way to deal with static members of a class, refer http://php.net/manual/en/language.oop5.static.php

flint0131
11-22-2010, 01:46 PM
Thanks for that, I've got another problem..
I got this error :


Warning: mysql_connect() [function.mysql-connect]: Access denied for user 'ODBC'@'localhost' (using password: NO) in C:\xampp\htdocs\please\db.php on line 12
Error : No database selected


Here's what the code looks like now,


<?php
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
class db {
public $var_db_username, $var_db_password, $var_db_host, $var_db_used;
public function db_con($host, $username, $password) {
$this->var_db_username = $username;
$this->var_db_password = $password;
$this->var_db_host = $host;
mysql_connect($this->var_db_host, $this->var_db_username, $this->var_db_password);
}

public function db_select($dbname) {
$this->var_db_used = $dbname;
mysql_select_db($dbname) or die("Error : ". mysql_error());
}

public function __destruct() { }
}

class Signup extends db {
/* for sign up */
public function isUsernameAlreadyRegistered($username) {
parent::db_con($this->var_db_host, $this->var_db_username, $this->var_db_password);
parent::db_select($this->var_db_used);
$sql = "SELECT * FROM users WHERE username = $username";
if ( mysql_query($query) ) {
return true;
}
return false;
}

public function add_user($username, $password, $email, $bday, $firstname, $lastname) {
mysql_query("INSERT INTO users(username, password, email, birthday, firstname, lastname)
VALUES ($username, $password, $email, $bday, $firstname, $lastname)");
}
}
?>

idalatob
11-22-2010, 02:06 PM
I think the layout below is a bit easier to understand. I dont think its neccessary to mess around with static and all this fancy mumbo jumbo for now. By doing it this way it is easier to manage what you are doing! I see some weird stuff you're doing with your connection parameters, theres no need for all that assignment. Also try to do everything as simply as possible (at first) then add in the necessary complexity. (ie static, public, private, protected etc)



<?php
class db {
//store our connection variables here
//or we can avoid that an pass directly into function
private $con_vars = array("localhost","test","test");
public $link;

//boot strapper to make life easier for permissions
//ie if the function is not public bootstrapper must handle
public function bootStrapper($db){
$this->link = mysql_connect($this->con_vars[0],$this->con_vars[1],$this->con_vars[2]);
$this->selectDB($db);
}
private function selectDB($db){
mysql_select_DB($db,$this->link);
}
private function close(){
mysql_close($this->link);
}
}
class functions extends db{
function do_something($value){
//can call connect via here
/*like this*/parent::bootStrapper('test');
/*or like this*/$this->bootStrapper('another_test');
print $value;
}
}

$test = new functions;
//or externally
//$test->bootStrapper('test');
$test->do_something('Hello World');
?>


Remember that at any time during the execution of this program you will be connected to the database! you only need to close the con once you have run the functions you need:
ie:
1.) run boot strapper (connected to the db)
2.) run addUser (still connected) -> this function is stored in the functions (or whatever you want to call it)
3.) run $test->close() (not connected anymore)

Dormilich
11-23-2010, 07:55 AM
you can have a look at PHP’s database classes (PDO (http://php.net/pdo), MySQLi (http://php.net/mysqli)). there is really no need to combine mysql_* functions with OOP.

@idalatob: $test->close() is a candidate for __destruct()

flint0131
11-24-2010, 02:38 PM
Can you help me out with this one? It doesn't produce any error but can't add a data on my database.

Here's the class I'm using :


<?php
class Database {
public $dbhost, $dbusername, $dbpassword, $dbused, $dblink;
function __construct() {
$this->dbhost = "localhost";
$this->dbusername = "root";
$this->dbpassword = "";
}

function connectTo($dbused) {
$this->dbused = $dbused;
$this->dblink = mysql_connect($this->dbhost, $this->dbusername, $this->dbpassword) or die("Error : ". mysql_error());
}

function addUser($entry = array()) {
$this->connectTo($this->dbused);
if ( !$this->dblink ) {
die("Could not connect : ". mysql_error());
}
mysql_select_db($this->dbused);
$sql = "INSERT INTO users(username, password, email, birthday, firstname, lastname) VALUES(";
foreach ( $entry as $i => $value ) {
$sql .= "$value,";
}
$sql .= ")";
mysql_query($sql);
}

function __destruct() {
mysql_close($this->dblink);
}
}
?>


and this one is on my some .php file


<?php
require 'db.php';
$Database = new Database();
$toReg = array(
0 => $_POST["s_username"],
1 => sha1($_POST["s_password"]),
2 => $_POST["s_email"],
3 => $_POST["s_firstname"],
4 => $_POST["s_lastname"],
5 => $_POST["s_birthday"]
);
if ( isset($toReg[0]) ) {
$Database->connectTo("ufaceoff");
$Database->addUser($toReg);
}

?>

Dormilich
11-24-2010, 02:44 PM
I bet there is an SQL error (you don’t test whether mysql_query() succeeds). I’d go for a comma too much.

flint0131
11-24-2010, 03:23 PM
Updated codes, still it didn't work.. :(



<?php
class Database {
public $dbhost, $dbusername, $dbpassword, $dbused, $dblink;
function __construct() {
$this->dbhost = "localhost";
$this->dbusername = "root";
$this->dbpassword = "";
}

function connectTo($dbused) {
$this->dbused = $dbused;
$this->dblink = mysql_connect($this->dbhost, $this->dbusername, $this->dbpassword) or die("Error : ". mysql_error());
}

function addUser($entry = array()) {
$this->connectTo($this->dbused);
if ( !$this->dblink ) {
die("Could not connect : ". mysql_error());
}
mysql_select_db($this->dbused);
$sql = "INSERT INTO users(username, password, email, birthday, firstname, lastname) VALUES(";
foreach ( $entry as $i => $value ) {
if ( $i > sizeof($entry) ) {
$sql .= $value .", ";
} else {
$sql .= $value;
}
}
$sql .= ")";
mysql_query($sql);
}

function __destruct() {
mysql_close($this->dblink);
}
}
?>

Dormilich
11-24-2010, 03:25 PM
what does mysql_query() return?

you are aware that string input must be quoted in SQL? (Prepared Statements being the exception here)

flint0131
11-24-2010, 03:31 PM
It return nothing.. the page still remains the same.


foreach ( $entry as $i => $value ) {
if ( $i > sizeof($entry) ) {
$sql .= mysql_real_escape_string($entry[$i]) .", ";
} else {
$sql .= mysql_real_escape_string($entry[$i]);
}
echo mysql_real_escape_string($entry[$i]) ."\n";
}

I tried echoing and see if the function reads the array $entry, it prints though.. the problem might be on teh SQL. :/

Dormilich
11-24-2010, 03:33 PM
as I (and many others) said: mysql_query($sql) or die(mysql_error());

flint0131
11-24-2010, 03:45 PM
Error : Column count doesn't match value count at row 1

row 1 is for the password, and I used sha1() to encrypt it and store it as VARCHAR on my database.



------------

Edited :
Should I convert the password to SHA1 on PHP or in SQL?

Dormilich
11-24-2010, 04:06 PM
does the varchar has the right size? (SHA1 usually requires exactly 40 characters)

you can hash it on either side. should you want to use secure hash algorithms (SHA256, RIPEMD160, etc.), you need to use PHP.

flint0131
11-24-2010, 04:20 PM
Thanks.. got my code working. ^_^



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum