Access to MySql

0

Before migrating to the ".xyz" domain in Hostinger, the following script would function normally. You can not access the data now.

Why do not you display the total number of records? - Line 32 Why does the While loop not work? - Line 38

<!DOCTYPE html>    <!   teste_conexao.php  ><html>
<head>
    <meta charset="UTF-8"/>
</head>
<?php

date_default_timezone_set('Brazil/East');

$host = "mysql.hostinger.com.br";
$usuario = "u340822322_rfmc";
$senha_conect = "n6XrRbxHFQ1T";
$bd = "u340822322_pcl1";

$conn = mysqli_connect($host, $usuario, $senha_conect, $bd);



if($conn){
    echo "conectado"."<br>";
}else{
    echo "nao conectado"."<br>";
    die("Connection failed: " . mysqli_connect_error());
    exit;
}

$data = date('d/m/y - H:i:s'); 
echo "<br>" . $data . "<br><br>"; 

echo "<br>passou aqui - 01";

$selecao = mysqli_query('SELECT * FROM tabela_precos');

$total = mysqli_num_rows($selecao); // numero de registros do arquivo
echo "<br>===<br> Nº de registros = " . $total . "<br>===<br>";

$linha=array();
$n=0;
echo "<br>passou aqui - 02";

while ($dados = mysqli_fetch_array($selecao)) {
    echo "<br>passou aqui - 03";
    $linha[]=$dados;

    $linha1=$linha[$n]['nome_picole'];

    echo $linha1 . "<br>";
    $n++;
}

echo "<br>passou aqui - 04";

?>
<br>
finalizou aqui.
</html>

The result is:

conectado

16/10/18 - 08:09:03


passou aqui - 01
===
Nº de registros = 
===

passou aqui - 02
passou aqui - 04
finalizou aqui.

1) Why do not you display the total number of records? - Line 32 - The table has 17 registers 2) Why does the While loop not work? - Line 38

    
asked by anonymous 16.10.2018 / 13:49

3 answers

0

Expensive,

Has the database name inside the server not changed? It looks like you are connecting correctly in the database, but you are not accessing the database.

Check the name of the database in the database, and try to access it with the mysql_select_db command after the connection.

It would be something like

$conn = mysqli_connect($host, $usuario, $senha_conect, $bd);

if($conn){
    echo "conectado"."<br>";
}else{
    echo "nao conectado"."<br>";
    die("Connection failed: " . mysqli_connect_error());
    exit;
}

$db_selected = mysql_select_db('dbname', $conn);
if (!$db_selected) {
    die ('Não foi possível acessar o database dbname' . mysql_error());
}

Command documentation: link

    
16.10.2018 / 14:51
0

Try to use:

$selecao = mysqli_query('SELECT count(*) FROM tabela_precos');

fetch and it will return the number of records

    
17.10.2018 / 20:59
0

The query is failing because you did not pass the connection. As indicated in the manual , it would look like this:

$selecao = mysqli_query($conn, 'SELECT * FROM tabela_precos');
    
23.10.2018 / 16:35