dbh) && func_num_args() != 4) {return true;} if (!$this->dbh = mysql_connect($db_host,$db_user,$db_pass)) { echo get_class($this).'::connect() could not connect to server: ' . mysql_error(); return false; } else { if (!mysql_select_db($db_name, $this->dbh)) { echo get_class($this).'::connect() could not connect to database: ' . mysql_error(); return false; } else { return true; } } } function query($sql) { /* auto-connect to database */ if (!$this->dbh) {$this->connect();} if (!$this->results = mysql_query($sql, $this->dbh)) { echo get_class($this) . '::query() could not run query: ' . mysql_error() . '(SQL: ' . $sql . ')'; return false; } else { return true; } } function fetch_array($qid = '') { if($qid == NULL) { $r = mysql_fetch_array($this->results); } else { $r = mysql_fetch_array($qid); } if(is_null($r)) { echo get_class($this) . '::fetch_array() could not run: ' . mysql_error(); } else { return $r; } } function fetch_assoc($qid = '') { if($qid == NULL) { $r = mysql_fetch_assoc($this->results); } else { $r = mysql_fetch_assoc($qid); } if(is_null($r)) { echo get_class($this) . '::fetch_assoc() could not run: ' . mysql_error(); } else { return $r; } } function fetch_object($qid = '') { if($qid == NULL) { $r = mysql_fetch_object($this->results); } else { $r = mysql_fetch_object($qid); } if(is_null($r)) { echo get_class($this) . '::fetch_object() could not run: ' . mysql_error(); } else { return $r; } } function end() { mysql_free_result($dbh); } } /* Usage $sql = "SELECT * FROM table WHERE field='foobar'"; $mysql = new mysql(); $mysql->connect(); $mysql->query($sql); while ($r = $mysql->fetch_object()) { $c++; echo "$c - " . $r->field . "
"; } $mysql->end(); */ ?>