This is a discussion on which connection method? within the alt.comp.lang.php forums, part of the PHP Programming Forums category; Hi, I started with php and mysql and i discovered three methods for connecting to a database: $conn="DRIVER={...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
Hi,
I started with php and mysql and i discovered three methods for connecting to a database: $conn="DRIVER={MySQL ODBC 3.51 DRIVER};SERVER=10.0.0.181;DATABASE=reserv"; $connect=odbc_connect($conn,'root','pw'); include('/inetpub/wwwroot/resphpaccess/adodb/adodb.inc.php'); $db = &ADONewConnection('mysql'); $db->Connect('localhost', 'root', 'pw', 'reserv'); $link = mysql_connect("localhost", "mysql_user", "mysql_password") or die("Impossible de se connecter : " . mysql_error()); Which one is the best and why? What are the pro/contras for each? Thanks Ben |
|
|||
|
Pros and Cons all involve personal choice. When in doubt, go for the
easiest to debug option, and the option that gives you full control over the source. :-) mysql_connect(blahblah) is my preferred choice, but you should roll your own DB class for many reasons. Here is my favorite implementation of a mysql class: class Abstract_DB_MYSQL { private $connected = false; private $connection; private $link; private $result; private $records; private $record; private $current_row; private $last_query; public function __construct($host, $db, $user, $pass) { $this->connection = mysql_connect($host, $user , $pass); $this->connected = mysql_select_db($db, $this->connection); if (!$this->connected) { trigger_error("Unable to connect to database.", E_USER_WARNING); } return $this->connected; } public function query($query) { if (!$this->connected) { return false; } $this->last_query = $query; $this->result = mysql_query($query, $this->connection); return $this->result; } public function next_record() { if (!$this->result) { return false; } $this->record = mysql_fetch_assoc($this->result); $this->records[] = $this->record; return $this->record; } public function all_records() { return $this->records; } public function f($key, $default = '') { if (isset($this->record[$key])) { return $this->record[$key]; } else { trigger_error("Key `$key` not found in recordset. Returning default `$default` instead.", E_USER_WARNING); return $default; } } public function nf() { if (!$this->result) { return 0; } return max(mysql_num_rows($this->result), mysql_affected_rows($this->connection)); } public function last_insert_id() { return mysql_insert_id($this->connection); } // Escapes a string using a method safer than addslashes() public function escape($string) { return mysql_escape_string($string); } // Returns a properly quoted table or field name public function qk($key) { return '`' . $key . '`'; } // Returns a properly quoted and escaped value public function qv($value, $escape = true) { if ($escape) { return "'" . $this->escape($value) . "'"; } else { return "'" . $value . "'"; } } } To use this class: $db = new Abstract_DB_MYSQL($host, $db, $user, $pass); // Query $db->query("select * from $table where $field = $value"); // How many rows returned? echo "Rows returned: " . $db->nf() . "<br><br>"; // TIP: $db->nf() also returns affected rows after an update. // Iterating through results: while($assoc = $db->next_record()) { echo '<pre>' . print_r($assoc) . '</pre><br><br>'; // Optionally, use the class on individual fields: echo $db->f('some_field'); } // Escaping parameters and auto-quoting for mysql: $new_field = $db->qk($field); $new_value = $db->qv($value); // Or you could manually escape without quotes: $string = $db->escape($string); The benefit to using a class like this (with very generic method names) is that later on you could roll a Abstract_DB_PGSQL class 5 years from now and have instant PostgreSQL support in your application without recoding. "Ben" <sdvs@qcsq> wrote in message news:3uGdnXVoi7KbuT7eRVnytQ@scarlet.biz... > Hi, > > I started with php and mysql and i discovered three methods for connecting > to a database: > > > $conn="DRIVER={MySQL ODBC 3.51 DRIVER};SERVER=10.0.0.181;DATABASE=reserv"; > $connect=odbc_connect($conn,'root','pw'); > > include('/inetpub/wwwroot/resphpaccess/adodb/adodb.inc.php'); > $db = &ADONewConnection('mysql'); > $db->Connect('localhost', 'root', 'pw', 'reserv'); > > $link = mysql_connect("localhost", "mysql_user", "mysql_password") > or die("Impossible de se connecter : " . mysql_error()); > > > Which one is the best and why? > What are the pro/contras for each? > > Thanks > Ben > > |
|
|||
|
First is ODBC, second is ADO, third is native.
ODBC is an asbtraction layer, there exists ODBC drivers to many different types of databases. It is fast. It is an older method than ADO. ADO has some extra's, but is slower I believe. I personally have always preferred ODBC. The last option is native, it uses Mysql's own functions. This means it is faster than ODBC and ADO. The use of abstraction interfaces like ODBC and ADO are useful when you wish to remain flexible and communicate with other databases too. If you are planning to only use Mysql, then just use mysql_connect and other mysql_ functions. Alternatively, you can use a PHP abstraction class like those provided as PEAR classes (pear.php.net), which will use native functions depending on which one you use. If you are planing ot use PHP and Mysql only, I'd just use mysql_connect or go with one of the PEAR abstraction layers. ODBC is nice when you are developing for microsoft platform and you just want to use an ODBC profile that can be setup by the user via the control panel. ADO is very microsoft'ish, thus expect it to be slow and bloated and not as portable usually. Lisa "Ben" <sdvs@qcsq> wrote in message news:3uGdnXVoi7KbuT7eRVnytQ@scarlet.biz... > Hi, > > I started with php and mysql and i discovered three methods for connecting > to a database: > > > $conn="DRIVER={MySQL ODBC 3.51 DRIVER};SERVER=10.0.0.181;DATABASE=reserv"; > $connect=odbc_connect($conn,'root','pw'); > > include('/inetpub/wwwroot/resphpaccess/adodb/adodb.inc.php'); > $db = &ADONewConnection('mysql'); > $db->Connect('localhost', 'root', 'pw', 'reserv'); > > $link = mysql_connect("localhost", "mysql_user", "mysql_password") > or die("Impossible de se connecter : " . mysql_error()); > > > Which one is the best and why? > What are the pro/contras for each? > > Thanks > Ben > |
|
|||
|
Thanks
"Lisa Pearlson" <no@spam.plz> schreef in bericht news:43a46996$0$9430$e4fe514c@dreader18.news.xs4al l.nl... > First is ODBC, second is ADO, third is native. > ODBC is an asbtraction layer, there exists ODBC drivers to many different > types of databases. It is fast. It is an older method than ADO. ADO has > some extra's, but is slower I believe. I personally have always preferred > ODBC. > > The last option is native, it uses Mysql's own functions. This means it is > faster than ODBC and ADO. > > The use of abstraction interfaces like ODBC and ADO are useful when you > wish to remain flexible and communicate with other databases too. If you > are planning to only use Mysql, then just use mysql_connect and other > mysql_ functions. > > Alternatively, you can use a PHP abstraction class like those provided as > PEAR classes (pear.php.net), which will use native functions depending on > which one you use. > > If you are planing ot use PHP and Mysql only, I'd just use mysql_connect > or go with one of the PEAR abstraction layers. > ODBC is nice when you are developing for microsoft platform and you just > want to use an ODBC profile that can be setup by the user via the control > panel. > > ADO is very microsoft'ish, thus expect it to be slow and bloated and not > as portable usually. > Lisa > > "Ben" <sdvs@qcsq> wrote in message > news:3uGdnXVoi7KbuT7eRVnytQ@scarlet.biz... >> Hi, >> >> I started with php and mysql and i discovered three methods for >> connecting to a database: >> >> >> $conn="DRIVER={MySQL ODBC 3.51 >> DRIVER};SERVER=10.0.0.181;DATABASE=reserv"; >> $connect=odbc_connect($conn,'root','pw'); >> >> include('/inetpub/wwwroot/resphpaccess/adodb/adodb.inc.php'); >> $db = &ADONewConnection('mysql'); >> $db->Connect('localhost', 'root', 'pw', 'reserv'); >> >> $link = mysql_connect("localhost", "mysql_user", "mysql_password") >> or die("Impossible de se connecter : " . mysql_error()); >> >> >> Which one is the best and why? >> What are the pro/contras for each? >> >> Thanks >> Ben >> > > |