1$host = "localhost";//Ip of database, in this case my host machine
2$user = "root"; //Username to use
3$pass = "qwerty";//Password for that user
4$dbname = "DB";//Name of the database
5
6try {
7 $connection = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass);
8 $connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
9
10}catch(PDOException $e)
11{
12 echo $e->getMessage();
13}
1<?php
2$db = "mysql:host=localhost;dbname=pdolearn";
3$user = "root";
4$password = "";
5
6$pdo = new PDO($db, $user, $password);
7
8// $stmt = "SELECT * FROM countries"; //for mySQL
9$stmt = $pdo->query("SELECT * from countries");
10
11// while($row = mysqli_fetch_assoc($stmt)) //for mysql
12while ($row = $stmt->fetch())
13{
14 echo "<pre>";
15
16 print_r($row);
17}
18
1<?php
2
3 $pdo = new PDO('mysql:host=localhost;
4 dbname=the_name_of_your_databe,
5 'username',
6 'password'');
7# by default your username is root
8# if you don't have a password don't fill in it
9
10#(optional) :
11 $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
12 $pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);
13
14?>
1$db = new PDO("sqlsrv:Server=YouAddress;Database=YourDatabase", "Username", "Password");
2
1// output headers so that the file is downloaded rather than displayed
2header('Content-Type: text/csv; charset=utf-8');
3header('Content-Disposition: attachment; filename=data.csv');
4
5// create a file pointer connected to the output stream
6$output = fopen('php://output', 'w');
7
8// output the column headings
9fputcsv($output, array('Column 1', 'Column 2', 'Column 3'));
10
11// fetch the data
12mysql_connect('localhost', 'username', 'password');
13mysql_select_db('database');
14$rows = mysql_query('SELECT field1,field2,field3 FROM table');
15
16// loop over the rows, outputting them
17while ($row = mysql_fetch_assoc($rows)) fputcsv($output, $row);
18