473,388 Members | 1,198 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

how to use the db class to other class....... i cannot display the records.

1
Expand|Select|Wrap|Line Numbers
  1.  
  2. <?PHP
  3. //THIS IS MY DB CLASS CONNECTION
  4. class db
  5. {
  6.      private $hostname;
  7.      private $username;
  8.      private $password;
  9.      private $database;
  10.      private $connect;
  11.      private $select_db;
  12.      public  $dbSql;
  13.  
  14.      public function db()
  15.      {
  16.           $this->hostname = "localhost";
  17.           $this->username = "root";
  18.           $this->password = "";
  19.           $this->database = "mydb";
  20.      }
  21.  
  22.      public function open_connection()
  23.      {
  24.           try
  25.           {
  26.                $this->connect = mysql_connect($this->hostname,$this->username,$this->password);
  27.                $this->select_db = mysql_select_db($this->database);
  28.           }
  29.           catch(exception $e)
  30.           {
  31.                return $e;
  32.           }
  33.      }
  34.  
  35.      public function close_connection()
  36.      {
  37.           try
  38.           {
  39.                mysql_close($this->connect);
  40.           }
  41.           catch(exception $e)
  42.           {
  43.                return $e;
  44.           }
  45.      }
  46. }
  47.  
  48. ?>
  49.  
  50. <?php
  51. //Class to use the db class....
  52. include('db.php');
  53.  
  54. class usr{
  55.  
  56.   private $mydb;
  57.  
  58.   $mydb = new db;//::open_connection();
  59.  
  60.   public function users_query()
  61.      {
  62.           try
  63.           {
  64.                //$this->$mydb->open_connection();
  65.                $mydb->dbSql = mysql_query("SELECT * FROM users");
  66.           }
  67.           catch(exception $e)
  68.           {
  69.                return $e;
  70.           }
  71.           $mydb->close_connection();
  72.           return $mydb->dbSql;
  73.  }
  74.  
  75. }
  76. ?>
  77.  
  78.  
  79.  
  80. <?php
  81. //AND MY OTHER CODES TO DISPLAY THE RECORDS...
  82.      //DISPLAY USERS***************************
  83.         include('..//controller/getrecord.php');
  84.         $grec=new grecord();
  85.         $grec->display_users();
  86.      //****************************************     
  87.     ?>
  88.  
  89.  
May 3 '11 #1
1 1447
Dormilich
8,658 Expert Mod 8TB
line #58: syntax error! a class property can only be initialised with a scalar or array, never with a function call or object initialisation.


class db: the outdated mysql extension will never throw an Exception, the only DB system that does that (which I know) is PDO.

class usr: there’s no constructor or dependency injector to get the db object.

if you want to dabble with databases and OOP, I strongly advise you to use PDO or MySQLi as they are already classes and as such more suited to be used in OOP.
May 3 '11 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Dennis C. Drumm | last post by:
Is there a way with C# to allow one class access to a method or field of another class, without making that method or field visible to all other classes, as would be the case when making the method...
2
by: yoshitha | last post by:
hi, i want to display records in datagrid. strtestids() is a string array. it contains some testids. based on these ids i've to fill the datagrid. i wrote code like this
0
by: Tony | last post by:
vs 2002 - vb - access 2002 - xp - web forms I want to display records, one at a time. I have about 25 fields to display. I've created SQL and retrieved records. I placed data in 25 separate...
3
by: Kai Zhang | last post by:
I am trying to display some database records in datagrid using dataset. the records need to be displayed are couple of thousands, but the records in database that the SQL query needs to exam are...
1
by: hameeduddinasim | last post by:
Hi i have 2 button on a web page(index.aspx) and both the button have the same code i.e. response.redirect("index.aspx?var=c") but when i click one button it is refreshing the page and the...
0
tjc0ol
by: tjc0ol | last post by:
Hi guys, I'm a newbie of this stuffs, We had a small office network (1 Windows 2K - Server) and (3 Windows XP - Client). I am testing to 1 PC (Windows 2K) installed with Licensed Wingate...
5
by: dorandoran | last post by:
How do I display records using object oriented method? All I have the link to their api help http://www.e-topco.com/oa/oa_help/OpenAir_SOAP.htm and few example. I am want display all the...
4
by: eschneider | last post by:
I get the following error when trying to browse the .asmx. I get the same thing when trying to add a reference. using .NET 2.0 There is no error message. Any ideas? Thanks,
8
by: jawad malik | last post by:
I want that each time i execute query it display records in a way that the top record will be positioned last and so on. First time when i executed query it will display like this name A B ...
0
by: Rahul Metha | last post by:
o Select table from drop down list. o Display records in Grid – Display all records belong to table in grid. o Table Fields – This section will have all the fields which are belongs to table.
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.