<?php/** * Declares an array to hold the database link information */$C = array ();/** * holds relevant information in the database link information array */$C [' db_host '] = ' localhost '; $C [' db_user '] = ' root '; $C [' db_pass '] = ' 123456 '; $C [' db_name '] = ' db_test '; /** * Cyclic Generation Constants * /foreach ($C as $name + = $val) { define ($name, $val);}/** * Link Database with PDO */ $dbo = n EW PDO ("mysql:host="). Db_host. ";d bname=". Db_name. "","" . Db_user. "","" . Db_pass. "") or Die ("Database link Failed"); /** * Try to write an example of a linked database * /try{$rs = $dbo, Prepare ("SELECT * from ' Test '); $rs->execute (); $results = $rs->fetchall (PDO::FETCH_ASSOC); $dbo = null; Print_r ($results);} catch (Exception $e) {print $e->getmessage (); exit;} /*end of File db.class.php*/
PHP PDO Connection Database