2
Guys I’m with this script but I can not connect to the database at all, please help me??
is working perfectly this way, more precise pull the date from the database.
Is returning these errors:
Fatal error: Uncaught Error: Call to a Member Function format() on Boolean in C: xampp htdocs listar listar_usuario.php:12 Stack trace: #0 {main} thrown in C: xampp htdocs listar listar_usuario.php on line 12
What’s in the database --> 2019-02-25 12:47:27
How it should present --> 1 day, 2 hours, 8 minutes and 4 seconds ago
<?php
include_once "conexao.php";
//consultar no banco de dados
$result_usuario = "SELECT DATE_FORMAT(data_acesso, '%Y-%m-%d-%H-%i-%s') as data_acesso FROM adms_ultimos_acessos ORDER BY id DESC;";
$resultado_usuario = mysqli_query($conn, $result_usuario) ;
$registro = mysqli_fetch_assoc($resultado_usuario ); // <--- esta linha
$adms_ultimos_acessos = preg_replace("(\:|\s)", "-", $registro['data_acesso']);
$adms_ultimos_acessos = str_replace(":", "-", $registro['data_acesso']);
$adms_ultimos_acessos = str_replace(" ", "-", $adms_ultimos_acessos);
$adms_ultimos_acessos = DateTime::createFromFormat('Y-m-d H:i:s', $registro['data_acesso']);
$adms_ultimos_acessos = $adms_ultimos_acessos->format('Y-m-d-H-i-s');
//Verificar se encontrou resultado na tabela "usuarios"
if(mysqli_num_rows($resultado_usuario) > 0){
?>
<?php
$adms_ultimos_acessos = $registro ['data_acesso']; // <-- aqui está o registro
$databd2 = date('Y-m-d-H-i-s');
$data1 = explode('-', $adms_ultimos_acessos);
$data2 = explode('-', $databd2);
$ano = $data2[0] - $data1[0];
$mes = $data2[1] - $data1[1];
$dia = $data2[2] - $data1[2];
$hora = $data2[3] - $data1[3];
$min = $data2[4] - $data1[4];
$seg = $data2[5] - $data1[5];
// configuração data
if ($mes < 0) {$ano--; $mes = 12 + $mes; }
if ($dia < 0) { $mes--; $dia = 30 + $dia; }
if ($ano > 0) { $str_ano = $ano . ' ano'; }
if ($ano > 1) { $str_ano .= 's '; }
if ($mes > 0) { @$str_mes .= $mes . ' mes'; }
if ($mes > 1) {
if ($ano > 0) { $str_ano .= ', '; } $str_mes .= 'es'; }
if ($dia > 0) { $str_dia = $dia . ' dia'; }
if ($dia > 1) {
if ($mes > 0) { $str_mes .= ', '; } $str_dia .= 's'; }
// configuração hora
if ($min < 0) {$hora--; $min = 60 + $min; }
if ($seg < 0) { $min--; $seg = 60 + $seg; }
if ($hora > 0) { $str_hora = $hora . ' hora'; }
if ($hora > 1) { @$str_hora .= 's'; }
if ($min > 0) { @$str_min .= $min . ' minuto'; }
if ($min > 1) {
if ($hora > 0) { @$str_hora .= ', '; } @$str_min .= 's'; }
if ($seg > 0) { $str_seg = $seg . ' seg'; }
if ($seg > 1) {
if ($min > 0) { $str_min .= ' e '; } $str_seg .= 's'; }
?>
<table class="table table-striped table-bordered table-hover">
<thead>
<tr>
<th>Data acesso</th>
</tr>
</thead>
<tbody>
<?php
while($row_usuario = mysqli_fetch_assoc($resultado_usuario)){
?>
<tr>
<th><?php echo $row_usuario['data_acesso']; ?></th>
<th><?php echo @$str_ano, @$str_mes, @$str_dia,', ', @$str_hora, @$str_min, @$str_seg, ' atrás'; ?>
</tr>
<?php
}?>
</tbody>
</table>
<?php
}else{
echo "<div class='alert alert-danger' role='alert'>Nenhum usuário encontrado!</div>";
}
This is the complete code ? because it has nothing to do with the database connection.
– Bulfaitelo
Yes I edited, the connection I made but I can’t pull the data from there understand? I’m not getting to do the SELECT* part and insert in $databd1
– Wesley Roberto
From what I understand I need to take the value of the query and insert in $databd1 is not?
– Wesley Roberto
Yes, your select will return only this value to be worked on the date?
– Bulfaitelo
What date do you need to pull from the bank? Date of a record?
– Andrei Coelho
But what is the name of the date field? What is the name of the table?
– Andrei Coelho
this exactly will work similarly to the time the comment was added, only I can’t create that part of SELECT on the date of registration, if you have an example using that code of mine or have some other solution that works in a similar way, give me the link please, thank you from now on
– Wesley Roberto
You need to be clearer Wesley. It’s not giving to understand what you want.
– Andrei Coelho
table:search field:databd1
– Wesley Roberto
$register = mysqli_fetch_assoc($query); instead of $query it would be $resultado_usuario, this is one of the first errors
– Gabriella Selbach
With the help of Andrei I modified some things I will post a new edition there
– Wesley Roberto
Hello there as it stood and the mistakes of the return.
– Wesley Roberto
The format of the date saved in the seat?
– Costamilam
He is saved like this: 2019-02-25 12:47:27 datetime
– Wesley Roberto
I think the error is in the structure that he is getting the date
– Wesley Roberto