Access to Mysql

Asked

Viewed 80 times

0

Before migrating to the ". Xyz" domain on Hostinger, the following script worked normally. Now it does not access the data.

Why not 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 does it not display the total number of records? - Line 32 - The table has 17 records 2) Why does the While loop not work? - Line 38

  • Try to edit your question, it’s a mess. We don’t know the lines of the code you’re talking about, put a commit saying "problem 1)" or something like that. And explain better what the problem is.

  • In the meantime if you’re trying to change domains you also had to change the database? Then you have to import the database tables.

3 answers

0

Expensive,

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

Check the database name in the database, and try to access it with the command mysql_select_db 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: http://php.net/manual/en/function.mysql-select-db.php

  • I created a new database, in the new domain. And what is happening is this: connects correctly, but does not access the data.

  • And did you experience eating for database selection? It worked?

  • I tried it. It’s still the same.

0

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

$selecao = mysqli_query($conn, 'SELECT * FROM tabela_precos');
  • So it worked. Thank you very much. Sorry for the delay in responding, but I ran out of internet for many days. Thank you very much.

0

Try to use:

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

give the fetch and it will return the number of records

  • It didn’t work, buddy. It says it’s connected, but it doesn’t give access to the table.

Browser other questions tagged

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