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?

Link to comment
https://forums.phpfreaks.com/topic/288531-classes/
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
https://forums.phpfreaks.com/topic/288531-classes/#findComment-1479692
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
https://forums.phpfreaks.com/topic/288531-classes/#findComment-1479693
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.