Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: błąd przy inkrementacji zmiennej
Forum PHP.pl > Forum > Przedszkole
bela
  1. <?php
  2. /*
  3.  * klasa mysql
  4.  */
  5.  
  6. class mysql {
  7. var $conn_id;
  8. var $result;
  9. var $number_queries = 0;
  10.  
  11. function mysql($host, $user, $pass, $db) {
  12. $this->conn_id = mysql_connect($host, $user, $pass) or die(&#092;"Brak po??czenia\");
  13. mysql_select_db($db) or die(&#092;"Nie mo?na wybra? bazy\");
  14. }
  15.  
  16. function query($query) {
  17. $this->number_queries++;
  18. return $this->result = mysql_query($query);
  19. }
  20.  
  21. function get_number_queries() {
  22. return $this->number_queries;
  23. }
  24.  
  25. }
  26.  
  27. ?>


dlaczego po wykonaniu zapytania, podczas pobrania liczby zapytan przez get_number_queries() jest wracane 0?
Puciek
Hmm, wygląda na to że musisz pare razy definiować klasę mysql, ponieważ ta funkcja działa tylko w obrębie tej wygenerowanej ($klasa = new mysql) niezapamieta liczenia jezeli wywoal potem ja znowu do tej zmiennej lub do innej winksmiley.jpg
bela
hmm, nie zrozumiałem ciebie za bardzo dry.gif , jak to można zrobić prawidłowo ?
scanner
Puciek: kłamiesz.
bela_666: pokaż jak wywołujesz powyższą klasę.
bela
o tak:
  1. <?php
  2. /**
  3. * J?dro sytemu
  4. * @package belacms
  5. * @author bela_666
  6. */
  7.  
  8.  
  9. require_once(&#092;"inc/config.php\");
  10. require_once(&#092;"inc/mysql.php\");
  11.  
  12.  
  13. class kernel {
  14.  
  15.         /**
  16.     * Referencja modu?u
  17.     */
  18.         var $module;
  19.         var $mod;
  20.     
  21.     var $config;
  22.     var $db;
  23.  
  24.     /**
  25.     * Konstruktor
  26.     * ?aduje modu?
  27.     */
  28.         function kernel() {
  29.  
  30.             $this->config = new config();
  31.         $this->db = new mysql($this->config->dbhost, $this->config->dbuser, $this->config->dbpass, $this->config->dbname);
  32.     }
  33. }
  34. ?>


a potem
  1. <?php
  2.  
  3. $smarty->assign(&#092;"number_queries\", $kernel->db->number_queries);
  4.  
  5. ?>

pokazuje 0
scanner
W Twoim kodzie nie widzę:
a) wywoąłnia metody ->query();
cool.gif wywołania metody ->get_number_queries();

Więc jak to ma działać?
bela
  1. <?php
  2. /**
  3. * J?dro sytemu
  4. * @package belacms
  5. * @author bela_666
  6. */
  7.  
  8.  
  9. require_once(&#092;"inc/config.php\");
  10. require_once(&#092;"inc/mysql.php\");
  11.  
  12.  
  13. class kernel {
  14.  
  15. /**
  16. * Referencja modu?u
  17. */
  18. var $module;
  19. var $mod;
  20.  
  21. var $config;
  22. var $db;
  23.  
  24. /**
  25. * Konstruktor
  26. * ?aduje modu?
  27. */
  28. function kernel() {
  29.  
  30. $this->config = new config();
  31. $this->db = new mysql($this->config->dbhost, $this->config->dbuser, $this->config->dbpass, $this->config->dbname);
  32. }
  33.  
  34. function load_module($module) {
  35. if($this->check_module) {
  36. require_once(modules_dir . $module . &#092;".php\");
  37. $this->module = new $module;
  38. $this->module->start();
  39. } else if(empty($module)) {
  40. $module = &#092;"root\";
  41. }
  42.  
  43. $this->mod = &$module;
  44. }
  45.  
  46. function check_module($module) {
  47. $db->query(&#092;"select * from modules where name='\" . $module . \"'\");
  48. if($db->num_fields() == 1) {
  49. return true;
  50. } else {
  51. return false;
  52. }
  53. }
  54. }
  55. ?>

  1. <?php
  2. /*
  3. * ustawienia
  4. */
  5. header(&#092;"Content-type: text/html; charset=iso-8859-2\");
  6.  
  7. define(&#092;"inc_dir\", \"inc/\", true);
  8. define(&#092;"modules_dir\", \"modules/\", true);
  9. define(&#092;"SMARTY_DIR\", \"/home/bela/public_html/mmcms/smarty/\");
  10. define(&#092;"home_dir\", \"/home/bela/public_html/mmcms/\");
  11.  
  12. /*
  13. * funkcja licząca czas wygenerowania strony
  14. */
  15. function get_microtime() {
  16.         list($usec, $sec) = explode(&#092;" \",microtime());
  17.         return ((float)$usec + (float)$sec);
  18. }
  19.  
  20. $time_on_start = get_microtime();
  21.  
  22. /*
  23. * smarty
  24. */
  25.  
  26. require_once(SMARTY_DIR . &#092;"Smarty.class.php\");
  27. $smarty = new Smarty();
  28. $smarty->template_dir = SMARTY_DIR . &#092;"templates/\";
  29. $smarty->compile_dir = SMARTY_DIR . &#092;"templates_c/\";
  30. $smarty->config_dir = SMARTY_DIR . &#092;"configs/\";
  31. $smarty->cache_dir = SMARTY_DIR . &#092;"cache/\";
  32.  
  33. /*
  34. * jądro systemu
  35. */
  36.  
  37. require_once(inc_dir . &#092;"kernel.php\");
  38. $kernel = new kernel();
  39. $kernel->load_module($_GET['id']);
  40. $smarty->assign(&#092;"module\", $_GET['id']);
  41. $smarty->assign(&#092;"mod\", $kernel->mod);
  42.  
  43. $kernel->config->get_vars();
  44.  
  45. $time_on_end = get_microtime();
  46. $time_to_display = round($time_to_display = $time_on_end - $time_on_start, 4);
  47.  
  48. $smarty->assign(&#092;"title\", $kernel->config->vars[\"title\"]);
  49. $smarty->assign(&#092;"gen_time\", $time_to_display);
  50. $smarty->assign(&#092;"number_queries\", $kernel->db->number_queries);
  51.  
  52. $smarty->display(&#092;"index.tpl\");
  53. ?>


tu chyba one są dry.gif
scanner
Wciąz nie widze tam wywołania ->query();
Nie dziwne że masz 0, skoro metoda w której incrementujesz nie jest wywołana...



------ UPDATE -----
Człowieku, może przestaniesz w kółko edytowąc swoje posty, tylko będzesz je pisał z rozumem?

Zobacz:
  1. <?php
  2. function check_module($module) {
  3. $db->query(&#092;"select * from modules where name='\" . $module . \"'\");
  4. if($db->num_fields() == 1) {
  5. return true;
  6. } else {
  7. return false;
  8. }
  9. }
  10. ?>


Pląćzesz się jak sznurek od tampaxa...

skoro:

var db;
$this->db->query();

to

$this->db->number_queries;

Pozatym coś mi się wydaje, ze z Obiektówką, to masz na bakier...
PO cholerę jakies instancje obiekty mysql w kernelu, w module...
niepotrzebnie zajmujesz pamięć i się plączesz w kodzie.
bela
popatrz jeszcze raz, edytowałem
scanner
Blah.
Jeszcze raz powiem, ze z tym edytowaniem to masz u mnie minusa.
Pisze sie posta tak, aby nie trzeba było edytować (dodając drugie tyle do niego).
bela
ups rolleyes.gif wezmę sobie to do serca biggrin.gif
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.