PHP & MySQL. Connect to database

11.5.3.4 use script language to connect a database 

PHP & MySQL database. Connect to database 

If you have created database in phphMyAdmin, you need connect to database all webpages of website.

For if better create separate file for connection:

File connect.php

<?php
    $username = 'root'; # default user 'root' in phpMyAdmin
    $userpassword = ''; # default no password of user 'root'
    $dbname = 'database_name'; # name of your database
    $servername = 'localhost'; # name of web server 
    # create variable of connection
    $conn = mysqli_connect($servername, $username, $userpassword,  $dbname);
    # check if no connect then stop script
    if(!$conn){
        die("Connection failed: " . mysqli_connect_error());
    }
    echo "Connected successfully";
?>

Check connection with database in webbrowser.

This file you can include to php files where will implement SQL queries.

Part 4. PHP. Create connection with database (in russian)

Subscribe to channel, follow for updates.

 

Категория: Relational databases | Добавил: bzfar77 (23.01.2023)
Просмотров: 2638 | Теги: server, username, userpassword, connect to database, php | Рейтинг: 5.0/1
Всего комментариев: 0
avatar