Monday 15 February 2010

oop - how to use a variable of one class in another in php -


I created a class named to connect to the server

  class db {private $ Host = 'localhost'; Personal $ username = 'root'; Private $ password = ''; Private $ DBC; Public Function Connect () {$ this- & gt; Dbc = mysql_connect_db ($ this-> host, $ this- & gt; username, $ this- & gt; password); }   

And in index.php I have created the object of class 'db'

  $ con = new db; $ Election & gt; Connect ();   

And the problem is that I created a query by

  mysql_query ($ query, "host connection parameter");   

And I do not know how to pass the second parameter for mysql_query () which is a host connection parameter. I tried typing

  mysql_query ($ query, $ election> connect () -> dbc);   

But that did not work. Anyone know how to fix this

Modify your class in this way so that you Can execute questions.

  class db {private $ host = 'localhost'; Personal $ username = 'root'; Private $ password = ''; Private $ DBC; Public Function Connect () {$ this- & gt; Dbc = mysql_connect_db ($ this-> host, $ this-> username, $ this-> password); } Public function query ($ sql) {return mysql_query ($ sql, $ this-> dbc); }   

Then you can use

  $ db = new db (); $ Db- & gt; Connect (); $ RS = $ db- & gt; Query ("SELECT .....");    

No comments:

Post a Comment