TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 09-30-2008, 07:15 AM   #1 (permalink)
The Addict
 
sarmenhb's Avatar
 
Join Date: Jan 2008
Location: los angeles
Posts: 309
Thanks: 44
sarmenhb is on a distinguished road
Default newb in OOP

trying to learn oop for once lol

here is what i am trying

Code:
<?php
class mysql_con {

var $host;
var $username;
var $password;
var $db;

function connect($host,$username,$password,$db) {

	mysql_connect("$this->host","$this->username","$this->password") or die(mysql_error());
	mysql_select_db("$this->db") or die(mysql_error());


}
}
?>
then in another file i did
Code:
<?php
include("myclass.inc"); //this is the classes file name.
$connect = new mysql_con();
$connect->connect("localhost","root","password","dbtest");
?>

is this how its done? although when i try it , i put the correct credentials into the login,password,db place but it says wrong password. this is what im getting

Code:
Warning: mysql_connect() [function.mysql-connect]: Access denied for user 'ODBC'@'localhost' (using password: NO) in C:\wamp\www\Appointment\myclass.inc on line 11
Access denied for user 'ODBC'@'localhost' (using password: NO)
could it be the "" that i've used??
__________________
no signature set

Last edited by sarmenhb : 09-30-2008 at 07:21 AM. Reason: code update
sarmenhb is offline  
Reply With Quote
Old 09-30-2008, 08:36 AM   #2 (permalink)
The Contributor
 
jcorradino's Avatar
 
Join Date: Sep 2008
Posts: 36
Thanks: 2
jcorradino is on a distinguished road
Default

does php even have a "this" keyword?
__________________
Jason Corradino
Applications Developer, Interactive Support - Tribune Technology
J2EE Development, Script Tinkering - Develop, Support, and Maintain Tribune websites.
jcorradino is offline  
Reply With Quote
Old 09-30-2008, 09:00 AM   #3 (permalink)
The Wanderer
 
Join Date: Feb 2008
Location: United Kingdom
Posts: 22
Thanks: 1
boycoda is on a distinguished road
Default

$connect->connect($host,$username,$password,$db);
Send a message via MSN to boycoda Send a message via Skype™ to boycoda
boycoda is offline  
Reply With Quote
Old 09-30-2008, 12:25 PM   #4 (permalink)
The Addict
 
CoryMathews's Avatar
 
Join Date: Nov 2007
Location: USA
Posts: 256
Thanks: 7
CoryMathews is on a distinguished road
Default

Quote:
does php even have a "this" keyword?
doesn't just about every language? I have yet to use it in php but I have not ever used a language that didnt have it.
CoryMathews is offline  
Reply With Quote
Old 09-30-2008, 03:45 PM   #5 (permalink)
The Contributor
 
jcorradino's Avatar
 
Join Date: Sep 2008
Posts: 36
Thanks: 2
jcorradino is on a distinguished road
Default

Quote:
Originally Posted by CoryMathews View Post
doesn't just about every language? I have yet to use it in php but I have not ever used a language that didnt have it.
I have yet to use it as well, and I thought it was in there. But I was trying to find it on php.net and I could not find anything (turns out "this" is a really hard thing to find :p)
__________________
Jason Corradino
Applications Developer, Interactive Support - Tribune Technology
J2EE Development, Script Tinkering - Develop, Support, and Maintain Tribune websites.
jcorradino is offline  
Reply With Quote
Old 09-30-2008, 05:46 PM   #6 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,381
Thanks: 5
Salathe is on a distinguished road
Default

PHP Manual: $this
Salathe is offline  
Reply With Quote
Old 09-30-2008, 06:51 PM   #7 (permalink)
The Addict
 
sarmenhb's Avatar
 
Join Date: Jan 2008
Location: los angeles
Posts: 309
Thanks: 44
sarmenhb is on a distinguished road
Default

so is anyone gonna answer my question? because it doesnt seem to work the way i have defined it :(
__________________
no signature set
sarmenhb is offline  
Reply With Quote
Old 09-30-2008, 06:56 PM   #8 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,381
Thanks: 5
Salathe is on a distinguished road
Default

Before the mysql_connect line you'll want to add:

PHP Code:
$this->host     $host;
$this->username $username;
$this->password $password;
$this->db       $db
Then they'll be available to use in the lines which follows.
Salathe is offline  
Reply With Quote
Old 10-08-2008, 10:14 AM   #9 (permalink)
The Frequenter
 
ReSpawN's Avatar
 
Join Date: Nov 2007
Location: Netherlands
Posts: 460
Thanks: 49
ReSpawN is on a distinguished road
Default

And usually, as an adittion to Salathe, you should never use $this-> in defining function variables. If it is a local function as in private and will be used in another one, it's fine, but you should not use it in the name itself, because $this defined the object and not passing a variable. Once defined IN the function, you can use $this. :)

As Salathe said, define first, then use. You can't set them manually and leave them blank.

The smartest thing to do, if you want this, is to create a simple function like mySQLinit(); and put every $this-> value in there, with a value (usually = '', = false or = -1).
__________________
"Life is a bitch, take that bitch on a ride"
Send a message via MSN to ReSpawN
ReSpawN is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 04:49 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design