Answers for "php connect with database"

PHP
18

php sql connection

<?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.";
?>
Posted by: Guest on May-11-2020
1

mysql connect php

# MySql improved
<?php
    $mysqli = new mysqli("localhost", "username", "password", "dbname");
	$result = $mysqli->query("SELECT lastname FROM employees");
?>
# Connection with PDO
<?php
    $myPDO = new PDO('mysql:host=localhost;dbname=dbname', 'username', 'password');
	$result = $myPDO->query("SELECT lastname FROM employees");
?>
# With PHP legacy functions:
<?php
    mysql_connect('localhost','username','password');
    mysql_select_db("dbname");
	$result = mysql_query('SELECT lastname FROM employees');
?>
Posted by: Guest on March-20-2021
1

database connection in php

<?php

$hostName = 'localhost';
$userNmame = 'root';
$password = '';
$dbname = 'topproduct';
$db_name = "mysql:host=$hostName;dbname=$dbname";
$conn = new PDO($db_name,$userNmame,$password);

if(!$conn){
    echo 'Error database connection';    
}
Posted by: Guest on June-01-2021

Browse Popular Code Answers by Language