Thursday, November 13, 2014

PHP For Working MySQL

              

This topic show you how to use PDO (PHP Data Object) to work with database.


  • How to connect to a MySQL database.
    • Creating a database object from PDO class.
      • example: new PDO($dsn,$username,$password),
    • Syntax for creating a data source name(DSN) for MySQL database.
      • mysql:host=host_address;dbname=yourdatabase.
    • For instance:Connect to a MySQL database named bookshop.
      • $dsn='mysql:localhost;dbname=bookshop'
      • $username='example'
      • $pass='example'
      • $db=new PDO($dsn,$username,$pass)
  • How to execute SELECT statement.
    • Once you have created PDO object you can use it's methods to execute SQL statements.To execute a SELECT statement, you use query method of PDO object.
    • A method of PDO class  for executing a SELECT statement.
      • query($select_statement).
    • For instance:
      • $query='SELECT *FROM Product';
      • $product=$db->query($query).
  • How to INSERT and UPDATE and DELETE statements.
    • A method for modifing the database.
      • exec($sql_statement).
    • For example:
      • $query='INSERT INTO Product VALUES ($id, '$name', '$description' )'.
      • $db->exec($query).

No comments:

Post a Comment