Answers for "how to connect php 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
3

db connection in php

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>";
    }


    ?>
Posted by: Guest on July-25-2020
0

php mysql connect

$host = 'localhost'; // URL to the server
$user = 'root'; // Username (xampp = root)
$pw = ''; // Password (xampp = )
$dbname = 'database'; // The name of your database 
$dsn = "mysql:host=$host;dbname=$dbname";
$options = [PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8']; // If you want to use utf-8 use this line

$db = new PDO($dsn, $user, $pw, $options); // Database Object
$db -> setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC); // Use this if you want an associate array
// $db -> setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC); Use this if you want an indexed array

$qry = 'select * from table;'; // Your query
$result = $db -> query($qry); // execute query

while ($row = $result -> fetch()) {
    $id = $row[/*column-name*/];
}
Posted by: Guest on March-03-2021

Code answers related to "how to connect php with database"

Browse Popular Code Answers by Language