Displaying dynamically created table records

Asked

Viewed 3,036 times

0

I set up a dynamic table with data from a query to Mysql, when I show all the records the table is displayed correctly, but when I request only one record the table does not display any record, see how it is:

    mysql_select_db($database_conCurriculo, $conCurriculo);
    $query_rsRegistro = "SELECT nome, email, celular, id_municipio, id_uf, dt_nascimento FROM candidato WHERE id_candidato = 158";
    $rsRegistro = mysql_query($query_rsRegistro, $conCurriculo) or die(mysql_error());
    $row_rsRegistro = mysql_fetch_assoc($rsRegistro);
    $totalRows_rsRegistro = mysql_num_rows($rsRegistro);


//Pegando os nomes dos campos
$numCampos = mysql_num_fields($rsRegistro);//Obtém o número de campos do resultado

for($i = 0;$i<$numCampos; $i++){//Pega o nome dos campos
    $Campos[] = mysql_field_name($rsRegistro,$i);
}

//Montando o cabeçalho da tabela
$tabela = '<table border="1"><tr>';

for($i = 0;$i < $numCampos; $i++){
    $tabela .= '<th>'.$Campos[$i].'</th>';
}

//Montando o corpo da tabela
$tabela .= '<tbody>';
while($r = mysql_fetch_array($rsRegistro)){
    $tabela .= '<tr>';
    for($i = 0;$i < $numCampos; $i++){
        $tabela .= '<td>'.$r[$Campos[$i]].'</td>';
    }
    $tabela .= '</tr>';

    echo $tabela;

}
exit;

//Finalizando a tabela
$tabela .= '</tbody></tabela>';

//Imprimindo a tabela
echo $tabela;
  • Are you sure there’s a record coming in, meaning there’s this 158 candidate?

  • Hello @bigown, yes this candidate exists, because I do a search in Mysql and it is there.

  • Ever tried with another value? Do a test like this: $query_rsRegistro = "SELECT \name`, `email`, `celular`, `id_municipio`, `id_uf`, `dt_nascimento` FROM `candidato` WHERE id_candidato = '158'";`

  • Hello @user3043340, yes it was one of the first tests I did, also not that.

  • Friend, your code appears to be correct, but I think it is not displaying any record because it has no record, I think your Query returns zero, do a test, try to check if the variable: $totalRows_rsRegister has some value before listing in the table, something else, try to run your Query inside the Database to see which return.

1 answer

2


I renewed my query by mounting and displaying the table in a different way, because I could not solve the problem of displaying the data from the previous one.

// Consultando candidados no banco
foreach($checkboxes as $id) {

    mysql_select_db($database_con, $con);
    $query_rsRegistro = "SELECT nome, email, celular, id_municipio, id_uf, dt_nascimento FROM candidato WHERE id_candidato = '$id'";
    $rsRegistro = mysql_query($query_rsRegistro, $con) or die(mysql_error());
    $row_rsRegistro = mysql_fetch_assoc($rsRegistro);
    $totalRows_rsRegistro = mysql_num_rows($rsRegistro);

    $Nome = $row_rsRegistro['nome'];
    $Email = $row_rsRegistro['email'];
    $Celular = $row_rsRegistro['celular'];
    $Municipio = $row_rsRegistro['id_municipio'];
    $UF = $row_rsRegistro['id_uf'];
    $Nascimento = $row_rsRegistro['dt_nascimento'];

    $tabela = "     
     <table width=100%  border=0 cellpadding=2 cellspacing=1>
      <tr>
        <td>Nome</td>
        <td>E-mail</td>
        <td>Celular</td>
        <td>Municipio</td>
        <td>UF</td>
        <td>Nascimento</td>
      </tr>";            
    do {            
      $tabela .= "
      <tr>
        <td>{$Nome}; </td>
        <td>{$Email}; </td>
        <td>{$Celular}; </td>
        <td>{$Municipio};</td>
        <td>{$UF};</td>
        <td>{$Nascimento};</td>     
      </tr>";
    } while ($row_rsRegistro = mysql_fetch_assoc($rsRegistro)); 
    $tabela .= "</table>";  

}
  • You can provide more information for your question via the edit link

  • 2

    In this case, the author of the question is not adding more information to the question, but is publishing the solution he arrived at. So, in my view, this is still a valid answer.

Browser other questions tagged

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