PHP function using PDO for databse

function db_connect()
{
    $dsn = "mysql:host=localhost;dbname=test;charset=utf8";
    $opt = array(
        PDO::ATTR_ERRMODE            => PDO::ERRMODE_EXCEPTION,
        PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
    );
    return new PDO($dsn,'root','', $opt);
}
 
 
$pdo = db_connect();

but note again - unlike with mysql_query(), you have to always use this $pdo variable for your queries.
 
you need not a boolean but PDO object. 
 
 
  • PDO is OO, meaning the connection to the database is represented by a PDO Object.
  • Your db_connect() function should return the object that gets created.
  • Passing in the parameters required by PDO will give you more flexibility!
 
Related Posts:
  • parent AND self PHP oops  parent  AND self PHP oops self::refers to the current class and it is usually used to accessstatic members, methods, and constants. parent::refers to the  parent class and it is most often used when wanting … Read More
  • PHP Interview Questions with Answers-part1    What is PHP?     PHP is a server side scripting language  used for web development applications.     Php is the powerful tool for making dynamic website.     Ma… Read More
  • PHP Session expire-minutes inactivity PHP Session expire-minutes inactivity session_cache_limiter('public'); session_cache_expire(15); //should expire after 15  minutes inactivity asy way to handle this, is to set a variable to $_SESSION  every time … Read More
  • How to Read an RSS Feed With PHP <?php include("../includes/config.php"); include("../includes/dbcon.php"); $tt_res=mysql_query("SELECT * FROM `rss_data`"); $num = mysql_num_rows($tt_res); if($num!=0) { $tt="TRUNCATE TABLE `rss_data`"; mysql_query($t… Read More
  • PHP isn’t as easy as working with JSON <?php $list = array( "eggs", "bread", "milk", "bananas", "bacon", "cheese" ); $xml = new SimpleXMLElement("<list />"); foreach($list as $item) { $xml->addChild("item", $item); } // for nice output $dom = dom_impo… Read More
  • PHP-final METHODS-override a final method However, there are times where you might want to make sure that a method cannot be re-implemented in its derived  classes. For this purpose, PHP supports the Java-like final access modifier for methods that declares … Read More
  • Aarray And multiple array values Aarray And multiple array values array[key_element1] => array(1, 2,3); array[key_element2] => array(4, 5, 6);  associative arrays array[key1] => array(key => value1, key => value2, key => value3); … Read More
  • Top codeigniter interview question and answers codeigniter interview question  What is codeigniter? Codeigniter is open source , web application framework.Its is for building websites using php.Codeigniter is loosely based on MVC pattern.Most simple framework in ph… Read More
  • Top PHP Tutorial website PHP - Wikipedia, the free encyclopedia PHP is a server-side scripting language designed for  web development but also used as a general-purpose programming language. As of January 2013, PHP was installed on ... http… Read More
  • PHP Sessions - setcookie mplement a session timeout of your own.  Both options mentioned by others session.gc_maxlifetime  and session.cookie_lifetime are not reliable. session.gc_maxlifetime session.gc_maxlifetime specifies the number o… Read More
  • Php Session Security-Internet Security Because a session may contain sensitive information, you need to treat  the session as a possible security hole. Session security is necessary to  create and implement a session. If someone is listening in or sno… Read More
  • Rewriting Keyword-Rich URLs rules for your .htaccess file Modify the .htaccessfile in your seophpfolder like this: RewriteEngine On # Rewrite numeric URLs RewriteRule ^Products/C([0-9]*)/P([0-9]*)\.html$ i /product.php?category_id=$1&product_id=$2… Read More
  • Mysql Join query Codeigniter Mysql Join query Codeigniter code loads and initializes the database class based on your configuration settings. $query = $this->db->query('SELECT name, title, email FROM my_table'); foreach ($query->result() … Read More
  • download a file by php code-PHP download files code Basic example for download a  file by php <?php $file="http://testexample.com/your_test_file.jpg"; // path to your file  header('Content-Type: application/octet-stream'); header('Content-Disposition: attachm… Read More
  • PHP INTERFACES Class inheritanceenables you to describe a parent-child relationshipbetween classes. For example, you might have a base class  Shapefrom which both Squareand Circlederive. However,  you might often want to add a… Read More