Select works on the localhost but does not work on the server

Asked

Viewed 833 times

2

The select was working normally on wamp, but when uploading the site to the server the information of some of the sites simply stopped coming.

The only part where the selectis working is the login of the administrative area, I have checked my connection file and all information matches the server, on select the same.

Follow the code from one of the pages :

include 'conexao.php';

// Selecionando informações da tabela galeriafotos
$sql = "SELECT * FROM `galeriafotos` order by datacadastro DESC";
//Selecionando informações da tabela projetos
$sql2 = "SELECT * FROM `projetos` order by datacadastro DESC";
$resultado = mysqli_query($conexao, $sql);

$resultado2 = mysqli_query($conexao, $sql2);

And call them on the page through the following code:

  <?php foreach($resultado as $linha) { ?>
       <tr>
       <td>
  <?php echo $linha["titulo"]; ?>
       </td>
       <td>
  <?php echo date('d/m/Y', strtotime($linha['datacadastro'])); ?>
        </td>
         <td>
         <?php echo $linha["categoria"]; ?>
          </td>

                                    <?php } ?>

3 answers

1

Try using PDO instead of mysqli_query.

for example: To make the connection.

 <?php $pdo = new PDO('mysql:host=localhost;dbname=seubancodedados', 'root', 'senha');pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);?>

Example of Select

<?php $consulta = $pdo->query("SELECT * FROM galeriafotos order by datacadastro DESC");?>

to display the result

<?php while ($linha = $consulta->fetch(PDO::FETCH_ASSOC)) {      echo Nome: {$linha['nome']} - Usuário: {$linha['usuario']}<br />";}?>
  • 1

    That doesn’t answer the question. The.o

  • I tried, but without result,...

1

You must heed the following:

  1. Checked if user, password, server and port are correct?
  2. The table is exactly the same as the development environment?
  3. There are records in the remote bank table?
  4. SQL works if you run it directly in the remote database?
  5. The application that is in the development environment is the same as wheel in the production environment?
  • Yes, for all of us...

  • your remote bank works for anything else? (Inserts, updates, etc)

  • Yes, normally...

0


By the demonstration of the code in question, it failed to do the fetch() so the print_r of $result displayed mysqli_result Object ( )

include 'conexao.php';

$sql = "SELECT * FROM `galeriafotos` order by datacadastro DESC";
$resultado = mysqli_query($conexao, $sql);

View archive

<?php foreach($resultado as $linha) { ?>

The correct is:

$sql = "SELECT * FROM `galeriafotos` order by datacadastro DESC";
$resultado = mysqli_query($conexao, $sql);

$itens = mysqli_fetch_all($resultado, MYSQLI_ASSOC);

Or alternatively:

$sql = "SELECT * FROM `galeriafotos` order by datacadastro DESC";
$resultado = mysqli_query($conexao, $sql);
$lista = array();
while($row = mysqli_fetch_assoc($resultado)){
   $lista[] = $row;
}

Browser other questions tagged

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