user2950593
0
Q:

php mysql connect

<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";

// Create connection
$conn= mysqli_connect($servername,$username,$password,$dbname);
// Check connection
if (!$conn) {
  die("Connection failed: " . mysqli_connect_error());
}
echo "Connected Successfully.";
?>
9
<?php
$servername = "localhost";
$username = "username";
$password = "password";

try {
    $conn = new PDO("mysql:host=$servername;dbname=myDB", $username, $password);
    // set the PDO error mode to exception
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    echo "Connected successfully";
    }
catch(PDOException $e)
    {
    echo "Connection failed: " . $e->getMessage();
    }
?>
2
$servername = "localhost";
$username = "username";
$password = "password";

// Create connection
$conn = new mysqli($servername, $username, $password);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";


Simplified

$conn = mysqli_connect('localhost', 'username', 'password');
$database = mysqli_select_db($conn, 'database');
16
<?php
$servername = "localhost";
$username = "username";
$password = "password";

// Create connection
$conn = new mysqli($servername, $username, $password);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
1

<?php

$con = mysqli_connect("localhost","my_user","my_password","my_db");

 
// Check connection

 if (mysqli_connect_errno()) {

  echo "Failed to connect to MySQL: " . mysqli_connect_error();

    
  exit();
}

?>
 
0
Just include this Temlate in other file using PHP Include/Require Keywords
 And Make Connection In One Shot :)

<?php
  
    // echo "Welcome to Connecting of DB Tutorial!";
    // echo "<br>";

    // 1. PDO - Php Data Objects
    // 2. MySQLi extension

    // Set Connection Variable
    $server = "localhost";
    $username = "root";
    $password = "";
    $database = "test";

    // Create A Connection
    $con = mysqLi_connect($server, $username, $password, $database);

     // Check For Connection
     if(!$con){
        die ("Connection Terminated! by Die() function". mysqLi_connect_error());
       
    }
    else {
        echo "Connection Succefully Happened! <br>";
    }


    ?>
3
<?php
$servername = "localhost";
$username = "yourusername"; // For MYSQL the predifined username is root
$password = "yourpassword"; // For MYSQL the predifined password is " "(blank)

// Create connection
$conn = new mysqli($servername, $username, $password);

 
// Check connection

 if ($conn->connect_error) {

    die("Connection failed: " . $conn->connect_error);
}

echo "Connected successfully";

?>
0

New to Communities?

Join the community