Câu lạc bộ Hỗ Trợ Học Tập
Bạn có muốn phản ứng với tin nhắn này? Vui lòng đăng ký diễn đàn trong một vài cú nhấp chuột hoặc đăng nhập để tiếp tục.



 
Trang ChínhTrang Chính  Latest imagesLatest images  Tìm kiếmTìm kiếm  Đăng kýĐăng ký  Đăng NhậpĐăng Nhập  
  • Top posters
 Mr.Pakapun (256)
 ddtan90 (178)
 tvduong (147)
 dthnam90 (137)
 minhquankq (101)
 arianbo (70)
 DoanhNhan (54)
 chicken (53)
 stormit (52)
 gentle_storm (47)

 

 [Help] - Class trong php

Go down 
2 posters
Tác giảThông điệp
minhquankq
Mod
Mod
minhquankq


Tổng số bài gửi : 101
Join date : 05/01/2011
Age : 32
Đến từ : Đại học cần thơ

[Help] - Class trong php Empty
Bài gửiTiêu đề: [Help] - Class trong php   [Help] - Class trong php EmptyWed May 16, 2012 9:39 am

Chào mọi người, hiện tại tôi đang có một rắc rối ngay lúc khởi đầu mong mọi người giúp đỡ...^^!
Tôi dự định làm một lớp kết nối với các hàm sẳn để cập nhật vào cơ sở dữ liệu luôn.
cụ thể như sao:
Code:
<?php
   class ketnoi
   {
      var $host;
      var $user;
      var $pass;
      var $database;
      var $conn;
      
      function ketnoi(){
         $this->$host="localhost";
         $this->$user="root";
         $this->$pass="";
         $this->$database="myblog";
         $this->$conn=mysql_connect($this->$host, $this->$user, $this->$pass);
         mysql_select_db($this->$database, $this->$conn);
         mysql_query("set names utf8");
         if (!defined('s_on')) {
            session_start();
            define('s_on', 'true');
         }
      }
      
      function addGopY($hoten, $email, $noidung)
      {
         $strIns = "INSERT INTO  `myblog`.`gop_y` (
               `ID_GY` ,
               `HOTEN_GY` ,
               `MAIL_GY` ,
               `NOIDUNG_GY`
               )
               VALUES (NULL ,  '".$hoten."',  '".$email."',  '".$noidung."')";
         mysql_query($strIns, $this->$conn) or die("Không thể gửi góp ý");
      }
   }
?>

sao đó qua file khác gọi như sao:

Code:
<?php include("connect.php"); ?>

<?php
   $connect=new ketnoi();
   $connect->addGopY("abc", "abccc", "ashd");
?>

nhưng lại bị lỗi, không thể thêm vào cơ sở dữ liệu được. mong mọi người giúp đỡ.
Về Đầu Trang Go down
http://AloneWithMe.co.cc
hhdang
Thành viên nhiệt tình
Thành viên nhiệt tình
avatar


Tổng số bài gửi : 29
Join date : 04/01/2011
Age : 33
Đến từ : sóc trăng

[Help] - Class trong php Empty
Bài gửiTiêu đề: Re: [Help] - Class trong php   [Help] - Class trong php EmptyTue Aug 14, 2012 9:56 pm

Hiện tại bạn khắc phục như sau nhé. Thay hàm khơi tạo ketnoi thành hàm __construct
Xem [You must be registered and logged in to see this link.]
vd của mình.


Code:
<?php
    class database{
        private $hostname = "localhost";
        private $username = "root";
        private $password = "vertrigo";
        private $dbname = "qldrl";
        private $connection;                       
        private $flagconn = false;
        private $query;
        private $result;
       
        /*-----------------------------------------------------------------------
        Function: Construct
        Parameter: No parameter
        Return: No return
        -------------------------------------------------------------------------*/
      public function __construct(){
           
            if (!$this->connection = mysql_connect($this->hostname, $this->username, $this->password))
          {
                die ("Không thể kết nối với server ");
            }
            else{
                if (!mysql_select_db($this->dbname,$this->connection)){
                    die ("Không thể kết nối với cơ sở dữ liệu ");
                    mssql_close($this->connection);
                }
                else{
                    mysql_query("SET CHARACTER SET utf8 ");
                    $this->flagconn = true;
                }
            }
        }
      
        /*---------------------------------------------------------
      set string query
      -------------------------------------------------------------*/
        public function setQuery($query)
        {
            $this->query = $query;
        }
       
      /*---------------------------------------------------------
      get string query
      -------------------------------------------------------------*/
        public function getQuery(){
            return $this->query;
        }
      
      /*---------------------------------------------------------
      get result of string query
      -------------------------------------------------------------*/
        public function getResult(){
            return $this->result;
        }
      
        /*---------------------------------------------------------
        Function: fetchAll
        Parameter: query string
        Return: array
        -------------------------------------------------------------*/
       public function fetchAll(){
            if ($this->flagconn == false){
                $this->__construct();
            }
            $this->result = mysql_query($this->query);
            return $this->result;
           
        }
      
      
      /*---------------------------------------------------------
        Function: isExits
        Parameter: table name , query string
        Return: false if table name or where string is empty, else return row number of ressult
        -------------------------------------------------------------*/
        public function isExits($table="", $where=""){
           
            if (empty($table) || empty($where))
            {
                return FALSE;
            }
            if ($this->flagconn == false){
                $this->__construct();
            }
            $this->setQuery("select * from $table where $where"); 
            $this->result = mysql_query($this->query);
            return mysql_num_rows($this->result);
        }
       
      /*---------------------------------------------------------
        Function: deleteRows
        Parameter: table name , query string
        Return: false if table name or where string  is empty, else return row number affected
        -------------------------------------------------------------*/
        public function deleteRows($table="", $where="")
        {
            if (empty($table) || empty($where))
            {
                return FALSE;
            }
            $this->query = "delete from $table where $where";
            $this->result = mysql_query($this->query, $this->connection);
            return  mysql_affected_rows($this->connection);
        }
      
       
      /*---------------------------------------------------------
        Function: insertRows
        Parameter: table name , atts array
        Return: false if table name or atts array is empty, else return row number affected
        -------------------------------------------------------------*/
      public function insertRows($table="", $atts="")
        {
            if(empty($table) || !is_array($atts))
            {
                return False;
            }
            else
            {
                while (list ($col, $val) = each ($atts))
                {
                    //if null go to the next array item
                    if ($val=="")
                    {
                        continue;
                    }
                    $col_str .= $col . ",";
                    if (is_int($val) || is_double ($val))
                    {
                        $val_str .= $val . ",";
                    }
                    else
                    {
                        $val_str .= "'$val',";
                    }
                }
                $this->query = "insert into $table ($col_str) values($val_str)";
                //trim trailing comma from both strings
                $this->query = str_replace(",)", ")", $this->query);
            }
            $this->result = mysql_query($this->query, $this->connection);
            return mysql_affected_rows($this->connection);
        }
       
      
      /*---------------------------------------------------------
        Function: updateRows
        Parameter: table name , atts array, string where
        Return: false if table name or atts array is empty, else return row number affected
        -------------------------------------------------------------*/
        public function updateRows($table="", $atts="", $where="")
        {
            if(empty($table) || !is_array($atts))
            {
                return FALSE;
            }
            else
            {
                while(list ($col, $val) = each ($atts))
                {
                    if ($val=="")
                    {
                        continue;
                    }
                    if(is_int($val) || is_double($val))
                    {
                        $str .= "$col=$val,";
                    }
                    elseif($val=="NULL" || $val=="null")
                    {
                        $str .= "$col=NULL,";
                    }
                    else
                    {
                        $str .= "$col='$val',";
                    }
                }
            }
            $str = substr($str, 0, -1);
            $this->query = "update $table set $str";
            if (!empty($where))
            {
                $this->query .= " where $where";
            }
            $this->result = mysql_query($this->query, $this->connection);
            return mysql_affected_rows($this->connection);
        }
       
        /*---------------------------------------------------------
        Function: execute query
        Parameter: query string
        Return: return 0 if ok, return 1 if fail
        Use:
            $db = new database();
            $db->setQuery("delete from NguoiDung where maSo='"."134567'");
            $db->executeQuery();
        ------------------------------------------------------------*/
        public function executeQuery(){
            if ($this->flagconn == false){
                $this->__construct();
            }
            $this->result = mysql_query($this->query);
            return mysql_affected_rows($this->connection);
        }
       
        /*---------------------------------------------------------
        function: destruct
        Parameter: No parameter
        return: No return
        */
        /*public function __destruct() {       
            if ($this->flagconn == true) {
                mssql_free_result($this->result);
                mssql_close ( $this->connection );
            }
   
        }*/
       
        public function numRecord(){
            if ($this->flagconn == false){
                $this->__construct();
            }
            $this->result = mysql_query($this->query) or die(mysql_error());
            $num = mysql_num_rows($this->result);
            return $num;           
        }
        public function getConnection(){
            return $this->connection;
        }
    }
?>
Về Đầu Trang Go down
 
[Help] - Class trong php
Về Đầu Trang 
Trang 1 trong tổng số 1 trang
 Similar topics
-
» Tạo mẫu tô cho nền trong pts
» Làm trong suốt Win 7
» Một số câu hỏi trong lập trinh web
» Tiếng Việt trong PHP
» Tạo report trong java

Permissions in this forum:Bạn không có quyền trả lời bài viết
Câu lạc bộ Hỗ Trợ Học Tập :: LẬP TRÌNH WEB :: PHP-
Chuyển đến