Connecting PHP with the MySQL database

1

I need help I'm trying to connect my PHP file with the bank so I can make a query and display the logs.

Connection code:

define('usuario_bd', 'root');
define('senha_bd', '');
define('nome_bd', 'loja');

mysql_connect('localhost/phpmyadmin', usuario_bd, senha_bd);
mysql_select_db(nome_bd);

$pesquisa = "SELECT * FROM nome";
$querybd = mysql_query($pesquisa);

while($correto = mysql_fetch_array($querybd)){
        echo $correto["nome"];
}

My server is the WampServer connection to phpMyAdmin.

    
asked by anonymous 15.08.2017 / 19:06

4 answers

4

PhpMyAdmin is not a database, and mysql does not connect via HTTP , mysql itself is already a protocol of its own TCP.

Difference of Mysql and Phpmyadmin

read this:

Do not use the old API

The functions that begin with mysql_ in the prefix are from the old API , read this:

How to use the MySqli API to connect to the Mysql database

The variable $host should only contain the HOST of your mysql server, that has nothing to do with Apache and nothing to do with HTTP:

<?php

$host = 'localhost';
$usuario = 'root';
$senha = 'minhasenha';
$banco = 'meubanco';

$link = mysqli_connect($host, $usuario, $senha, $banco);

if (mysqli_connect_error()) {
    printf('Erro de conexão: %s', mysqli_connect_error());
    exit;
}

if (!mysqli_set_charset($link, 'utf8')) {
    printf('Error ao usar utf8: %s', mysqli_error($link));
    exit;
}

if ($result = mysqli_query($link, 'SELECT * FROM nome')) {

    /* Pegando os dados */
    while ($row = mysqli_fetch_assoc($result)) {
        echo $row['nome'];
    }

    /* libera os resultados */
    mysqli_free_result($result);
} else {
    /*Trata o erro da query, se ocorrer*/
    printf('Erro na query: %s', mysqli_error($link));
    exit;
}

/* fecha a conexão */
mysqli_close($link);

Handling query error:

Your query is probably wrong, so you commented:

  

With mysqli another error occurs, in the case of Mysqli_fetch_array () expects parameter 1 to be mysqli_result, Boolean given - Otávio Guilherme

As the example I've done, there's an if and an else:

if ($result = mysqli_query($link, 'SELECT * FROM nome')) {
    ... SE OCORRER TUDO NORMAL ...
} else {
    ... SE TIVER UM ERRO NA QUERY ...
}

UTF-8

If you do not use UTF-8 remove this part:

if (!mysqli_set_charset($link, 'utf8')) {
    printf('Error ao usar utf8: %s', mysqli_error($link));
    exit;
}
    
15.08.2017 / 20:06
1

define('usuario_bd', 'root');
define('senha_bd', '');
define('nome_bd', 'loja');

$conexao = mysqli_connect('localhost', usuario_bd, senha_bd, nome_bd);
$retorno = mysqli_query($conexao,"SELECT * FROM nome");

while($row = mysqli_fetch_array($retorno)) {             
 echo $row["nome"];
    }  
    
15.08.2017 / 19:35
1

Friend, alternatively you can use the PDO, which is a more consistent way to use multiple databases.

The mysql_ * functions have been deprecated from PHP 7.0 if you still use an old version of PHP and need to update PHP for some reason you will have problems with these functions.

You can use the code snippet below to connect to a mysql database.

try {
        $dsn = sprintf("mysql:host=%s;dbname=%s;charset=utf8", $host, $dbname);
        $connection = new \PDO($dsn, $user, $password);
        $connection->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);


} catch (\PDOException $exc) {
     echo $exc->getTraceAsString();
}

Below is an example search with PDO:

$statement = $connection->prepare("SELECT * FROM tabela WHERE id = :id");
        try {
            $statement->bindValue( ':id', $id, \PDO::PARAM_INT );
            $statement->execute();


        $resultado = $statement->fetch(\PDO::FETCH_OBJ);

        } catch (\PDOException $exc) {
            throw new \Exception('Erro ao executar ' . $exc->getMessage());
        }
    
15.08.2017 / 19:49
-1

Try editing your "mysql_connect" line for this.

mysql_connect('localhost', usuario_bd, senha_bd);
    
15.08.2017 / 19:22