minhquankq Mod
Tổng số bài gửi : 101 Join date : 05/01/2011 Age : 32 Đến từ : Đại học cần thơ
| Tiêu đề: [Help] - Class trong php Wed 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 đỡ. | |
|
hhdang Thành viên nhiệt tình
Tổng số bài gửi : 29 Join date : 04/01/2011 Age : 33 Đến từ : sóc trăng
| Tiêu đề: Re: [Help] - Class trong php Tue 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; } } ?> | |
|