Jump to content

Classes


AndyPSV

Recommended Posts

Hello,

maybe back to basics, but

 

 pobierz, plaintext
  1. $db = new PDO('mysql:host=localhost;dbname=andypsv_cocain','root','',array(PDO::ATTR_PERSISTENT=>true)); $db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE,PDO::FETCH_ASSOC); define('PRFX','drug_');
  2.  
  3. class PAGE {
  4. var $_t,$_k,$_d,$_c,$_nav,$_,$tpl,$u,$md;
  5.  
  6. function PAGE($md='',$md2='',$md3='',$md4='') {
  7. $q = $db->query('SELECT * FROM `'.PRFX.'c0'.LG.'`'); if($q->rowCount() == 0) die(_E.'no categories'); while($_c0 = $q->fetch()) $c0[] = $_c0; $this->tpl->assign('c0',$c0);
  8. ...
  9.  
  10. PRZY CZYM... INNE KLASY.
  11.  
  12. class HELP extends PAGE {
  13. function i() {
  14. // I want to use here $db->query

How to handle this issue to use class $db inside other classes?

Up to this point I've used solely functions so it worked smoothly (i.e. mysql_query() alias).

How?

Edited by AndyPSV
Link to comment
Share on other sites

Using global $db is one way.

 

Another way I see often (and confused me because I didn't realize what was going on) is to realize that variables assigned in the global scope can be reached via the $GLOBALS['db'] super-variable. You may want to initially use $GLOBALS['db'] = new PDO().

Link to comment
Share on other sites

Why are you playing with classes and inheritance at this stage in your PHP development?  You have so many problems and apparently don't recognize proper syntax yet.  Just stick to writing some decent code and grow into classes later on, otherwise you will only be re-writing them again and again as you FINALLY begin to catch on.

 

1 - where is $db defined?

2 - you set your pdo to retrieve assoc arrays but then you try and retrieve the data with numeric indices.

3 - you fetch your entire result set and store it into an array to then pass that array on to some method.  Why?  Much smarter functions available to you but you haven't realized that yet.  Read the manual on 'fetchall'

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.