1<?php
2$servername = "localhost";
3$username = "username";
4$password = "password";
5$dbname = "myDB";
6
7// Create connection
8$conn= mysqli_connect($servername,$username,$password,$dbname);
9// Check connection
10if (!$conn) {
11 die("Connection failed: " . mysqli_connect_error());
12}
13echo "Connected Successfully.";
14?>
1<?php
2$servername = "localhost";
3$username = "username";
4$password = "password";
5
6try {
7 $conn = new PDO("mysql:host=$servername;dbname=myDB", $username, $password);
8 // set the PDO error mode to exception
9 $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
10 echo "Connected successfully";
11 }
12catch(PDOException $e)
13 {
14 echo "Connection failed: " . $e->getMessage();
15 }
16?>
1$servername = "localhost";
2$username = "username";
3$password = "password";
4
5// Create connection
6$conn = new mysqli($servername, $username, $password);
7
8// Check connection
9if ($conn->connect_error) {
10 die("Connection failed: " . $conn->connect_error);
11}
12echo "Connected successfully";
13
14
15Simplified
16
17$conn = mysqli_connect('localhost', 'username', 'password');
18$database = mysqli_select_db($conn, 'database');
1<?php
2 $user = "username";
3 $pass = "password";
4 $host = "host";
5 $dbdb = "database";
6
7$conn = new mysqli($host, $user, $pass, $dbdb);
8 if ($conn->connect_error) {
9 die("Connection failed: " . $conn->connect_error);
10}
11?>
1<?php
2$servername = "localhost";
3$username = "username";
4$password = "password";
5
6// Create connection
7$conn = new mysqli($servername, $username, $password);
8
9// Check connection
10if ($conn->connect_error) {
11 die("Connection failed: " . $conn->connect_error);
12}
13echo "Connected successfully";
14?>
1$host = 'localhost'; // URL to the server
2$user = 'root'; // Username (xampp = root)
3$pw = ''; // Password (xampp = )
4$dbname = 'database'; // The name of your database
5$dsn = "mysql:host=$host;dbname=$dbname";
6$options = [PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8']; // If you want to use utf-8 use this line
7
8$db = new PDO($dsn, $user, $pw, $options); // Database Object
9$db -> setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC); // Use this if you want an associate array
10// $db -> setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC); Use this if you want an indexed array
11
12$qry = 'select * from table;'; // Your query
13$result = $db -> query($qry); // execute query
14
15while ($row = $result -> fetch()) {
16 $id = $row[/*column-name*/];
17}