Prev: Need Simple PHP mysql search engine script
Next: PHP / iODBC / PostGreSQL: running into a deadlock
From: "win.a" on 23 May 2010 21:55 Hi my friend,i wrote two classed on is user,the other is db .The user have a property which from the db class object . When i writing my application have both class object invoked at the same time ,just like $user_a = new user($para); $db_a = new db() Then the problem occurs only one of the class object could work, or $user_a or $db_a not both,What 's problem of my code The following is my source code: class db{ /** * Constructor */ var $result; var $conn; function db(){ require_once BOC_BASE_DIR.'/config/db.config.inc'; $this->conn = mysql_connect($db_host,$db_user,$db_pass); //$this->conn = @mysql_connect($db_host,$db_user,$db_pass); if (!$this->conn) { $this->db_print_error("DB connect failed"); } if (!mysql_select_db($db_name,$this->conn)) { $this->db_print_error("DB select failed"); } } class user{ private $u_sn; private $u_id; private $u_name; private $u_sex; private $u_image; //more property private $u_info; /** * Constructor */ function __construct($name){ $sql = "select * from boc_user where u_name = '$name'"; $u_query = new db(); $this->u_info = array(); $u_query->db_query($sql); $this->u_info =$u_query->db_fetch_row(); $this->u_sn = $this->u_info['u_sn']; $this->u_id = $this->u_info['u_id']; $this->u_name = $this->u_info['u_name']; $this->u_sex = $this->u_info['u_sex']; //more are give the property value } The two class works well in the single application when only of them are invoked ,i cost me much time to deal it Any advice and suggestions are thankful ! All you best ------------------------ What we are struggling for ? The life or the life ?
From: Chris on 24 May 2010 00:22 On 24/05/10 11:55, win.a wrote: > Hi my friend,i wrote two classed on is user,the other is db .The user > have a property which from the db class object . > > > When i writing my application have both class object invoked at the > same time ,just like > $user_a = new user($para); > $db_a = new db() > > Then the problem occurs only one of the class object could work, or > $user_a or $db_a not both,What 's problem of my code > The two class works well in the single application when only of them > are invoked ,i cost me much time to deal it > Any advice and suggestions are thankful ! You'll need to include the 'db' class first if that's in a separate file before you include the 'user' class. Apart from that, what's the error you're getting? -- Postgresql & php tutorials http://www.designmagick.com/
From: "Tanel Tammik" on 24 May 2010 13:42 ""win.a"" <win.acc(a)gmail.com> wrote in message news:AANLkTinhmyybnAMtY5SX5m-FYoZTsywp1kxfnSexv8ke(a)mail.gmail.com... > Hi my friend,i wrote two classed on is user,the other is db .The user > have a property which from the db class object . > > > When i writing my application have both class object invoked at the > same time ,just like > $user_a = new user($para); > $db_a = new db() > > Then the problem occurs only one of the class object could work, or > $user_a or $db_a not both,What 's problem of my code > > The following is my source code: > > class db{ > /** > * Constructor > */ > > var $result; > var $conn; > > function db(){ > require_once BOC_BASE_DIR.'/config/db.config.inc'; > $this->conn = mysql_connect($db_host,$db_user,$db_pass); > //$this->conn = @mysql_connect($db_host,$db_user,$db_pass); > if (!$this->conn) { > $this->db_print_error("DB connect failed"); > } > > if (!mysql_select_db($db_name,$this->conn)) { > $this->db_print_error("DB select failed"); > } > } > > class user{ > private $u_sn; > private $u_id; > private $u_name; > private $u_sex; > private $u_image; > > //more property > > private $u_info; > > > /** > * Constructor > */ > function __construct($name){ > $sql = "select * from boc_user where u_name = '$name'"; > $u_query = new db(); > $this->u_info = array(); > $u_query->db_query($sql); > $this->u_info =$u_query->db_fetch_row(); > > $this->u_sn = $this->u_info['u_sn']; > $this->u_id = $this->u_info['u_id']; > $this->u_name = $this->u_info['u_name']; > $this->u_sex = $this->u_info['u_sex']; > > //more are give the property value > > } > > > The two class works well in the single application when only of them > are invoked ,i cost me much time to deal it > Any advice and suggestions are thankful ! > > > All you best > ------------------------ > What we are struggling for ? > The life or the life ? Hi, one way to do that: <?php class Database { private $link; function __construct() { $this->conn(); } function conn() { $this->link = mysql_connect(DB_HOST, DB_USER, DB_PASS); mysql_select_db(DB_NAME, $this->link); } function query($query) { return mysql_query($query, $this->link); } function fetch_assoc($result) { return mysql_fetch_assoc($result); } } class User { private $db; function __construct($db) { $this->db = $db; } function find_by_username($username) { $result = $this->db->query("select * from boc_user where username='$username'"); return $this->db->fetch_assoc($result); } } $db = new Database; $user = new User($db); $userdata = $user->find_by_username('username'); ?> I did not check the code, so there might be some typos. Anyway it should help you! Br Tanel
From: "Tanel Tammik" on 24 May 2010 13:43 ""win.a"" <win.acc(a)gmail.com> wrote in message news:AANLkTinhmyybnAMtY5SX5m-FYoZTsywp1kxfnSexv8ke(a)mail.gmail.com... > Hi my friend,i wrote two classed on is user,the other is db .The user > have a property which from the db class object . > > > When i writing my application have both class object invoked at the > same time ,just like > $user_a = new user($para); > $db_a = new db() > > Then the problem occurs only one of the class object could work, or > $user_a or $db_a not both,What 's problem of my code > > The following is my source code: > > class db{ > /** > * Constructor > */ > > var $result; > var $conn; > > function db(){ > require_once BOC_BASE_DIR.'/config/db.config.inc'; > $this->conn = mysql_connect($db_host,$db_user,$db_pass); > //$this->conn = @mysql_connect($db_host,$db_user,$db_pass); > if (!$this->conn) { > $this->db_print_error("DB connect failed"); > } > > if (!mysql_select_db($db_name,$this->conn)) { > $this->db_print_error("DB select failed"); > } > } > > class user{ > private $u_sn; > private $u_id; > private $u_name; > private $u_sex; > private $u_image; > > //more property > > private $u_info; > > > /** > * Constructor > */ > function __construct($name){ > $sql = "select * from boc_user where u_name = '$name'"; > $u_query = new db(); > $this->u_info = array(); > $u_query->db_query($sql); > $this->u_info =$u_query->db_fetch_row(); > > $this->u_sn = $this->u_info['u_sn']; > $this->u_id = $this->u_info['u_id']; > $this->u_name = $this->u_info['u_name']; > $this->u_sex = $this->u_info['u_sex']; > > //more are give the property value > > } > > > The two class works well in the single application when only of them > are invoked ,i cost me much time to deal it > Any advice and suggestions are thankful ! > > > All you best > ------------------------ > What we are struggling for ? > The life or the life ? Hi, one way to do that: <?php class Database { private $link; function __construct() { $this->conn(); } function conn() { $this->link = mysql_connect(DB_HOST, DB_USER, DB_PASS); mysql_select_db(DB_NAME, $this->link); } function query($query) { return mysql_query($query, $this->link); } function fetch_assoc($result) { return mysql_fetch_assoc($result); } } class User { private $db; function __construct($db) { $this->db = $db; } function find_by_username($username) { $result = $this->db->query("select * from boc_user where username='$username'"); return $this->db->fetch_assoc($result); } } $db = new Database; $user = new User($db); $userdata = $user->find_by_username('username'); ?> I did not check the code, so there might be some typos. Anyway it should help you! Br Tanel
|
Pages: 1 Prev: Need Simple PHP mysql search engine script Next: PHP / iODBC / PostGreSQL: running into a deadlock |