HTML and SQL connection

Asked

Viewed 5,191 times

2

I’m designing a database where I have a table with calculated data indices from other tables. I am also designing an HTML Dashboard to expose these indexes. My database is stored on a Mysql server. How do I extract this data and expose on Dashboard by linking the two technologies?

  • You will need some programming language to do this. I believe it is simpler and the one that most suits you is the [tag:php].

2 answers

6


As you’ve already spoken of PHP I’ll give a minimal example using PHP + Mysql.

Using PHP you should create a file .php and set in it your connection to the database.

Ex:

<?php
define("HOST", "nomedohost");    #Para o host com o qual você quer se conectar.
define("USER", "nomedousuario");          #O nome de usuário para o banco de dados. 
define("PASSWORD", "senha");           #A senha do banco de dados. 
define("DATABASE", "bancodedados");       #O nome do banco de dados. 
$mysqli = new mysqli(HOST, USER, PASSWORD, DATABASE);
?>

And then call him when you need to connect to the database.

To use a file PHP, the file you will call should be .php Ex: index.php

And in it you must request your file .php with the database, let’s imagine that file you created is called db.php.

As I see you already know SQL, I’ll give you an example using the Mysql.

Let’s imagine that your table is like this:

Nome da tabela: users
Colunas: username, profile
username: Foo
profile: é um cara legal.

PHP file with your HTML

<?php
    require_once('db.php');
    $username = "Foo";
    $sql = "SELECT * FROM `users` WHERE `username` = '$username'"; #sua query sql, um exemplo o SELECT *
    $query_exec = $mysqli->query($sql); #Executa o comando select.
    $row = $query_exec->num_rows; #Número de resultados encontrados.
    $get = $query_exec->fetch_array(); #Cria um vetor associativo com os dados.
    if($row > 0) {
        #encontrou resultados.
        $usuario = $get['username']; 
        $perfil = $get['profile'];
        echo "{$usuario} {$perfil}"; #Pode fazer um echo de um html, ex: echo '<div id="profile">'.$perfil.'</div>';
        #O resultado do echo seria: Foo é um cara legal.

    } else {
        #não encontrou nada.    
    }

?>
<!DOCTYPE html>
<html>
    <head>

    </head>
    <body>
    <!--Colocando as tags PHP para exibir o echo no HTML-->
    <div id="username"> <?php echo $usuario; ?> </div>
    </body>
</html>

There are many ways you can manipulate the echo of PHP, can do using AJAX, AJAX+Json, the web language ends up being very extensive, but as you need to learn to use the database, you can start doing some PHP course, and if you have no knowledge in programming, take a programming logic course.

5

You will not be able to make this connection with html. To do this you will need a programming language that supports this connection.

  • And what would this PHP link with HTML look like?

  • https://www.w3schools.com/php/showphpfile.asp?filename=demo_db_select_oo

  • 1

    OK! I already have an elementary knowledge of some languages front-end and back-end, but I’m improving! One of the ones I hadn’t counted on was PHP, but I’ve already started to take a look! Thank you very much!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.