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
3$hostName = 'localhost';
4$userNmame = 'root';
5$password = '';
6$dbname = 'topproduct';
7$db_name = "mysql:host=$hostName;dbname=$dbname";
8$conn = new PDO($db_name,$userNmame,$password);
9
10if(!$conn){
11 echo 'Error database connection';
12}
13
1# MySql improved
2<?php
3 $mysqli = new mysqli("localhost", "username", "password", "dbname");
4 $result = $mysqli->query("SELECT lastname FROM employees");
5?>
6# Connection with PDO
7<?php
8 $myPDO = new PDO('mysql:host=localhost;dbname=dbname', 'username', 'password');
9 $result = $myPDO->query("SELECT lastname FROM employees");
10?>
11# With PHP legacy functions:
12<?php
13 mysql_connect('localhost','username','password');
14 mysql_select_db("dbname");
15 $result = mysql_query('SELECT lastname FROM employees');
16?>
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}
1<?php
2 $serverName = "localhost";
3 $dBUsername = "root";
4 $dBPassword = "";
5 $dBName = "users";
6
7 $conn = mysqli_connect($serverName, $dBUsername, $dBPassword, $dBName);
8
9 if (!$conn) {
10 die("Connection failed: " . mysqli_connection_error());
11 }
12