I cannot display the SQL value

Asked

Viewed 187 times

6

I am trying to start a connection to the Mysql database and it works, but when I ask PHP to display what is there nothing appears. It’s like the connection to the BD had failed.

<?php
// definições de host, database, usuário e senha
$server = "";
$usuario = "";
$banco   = "";
$senha = "";
// conecta ao banco de dados
$conexao = mysql_connect($server, $usuario, $senha);
$conexao = mysql_select_db("$banco",$conexao);
if(!$conexao) {echo mysql_error(); exit;}

$conexao = mysql_query("select * from settingsMDP");
while($exibe = mysql_fetch_assoc($conexao));
?><?php echo $exibe["name"]; ?>
<form method="post" action="<?php $_PHP_SELF ?>">
<table width="400" border="0" cellspacing="1" cellpadding="2">
<tr>
<td width="100">Nome do MDP: </td>
<td><input name="name" type="text" id="name" value="<?php echo $exibe["name"]; ?>"></td>
</tr>
<tr>
<td width="100">Versão: </td>
<td><input name="name" type="text" id="name" value="<?php echo $exibe["version"]; ?>"></td>
</tr>
<tr>
<td width="100">Abertura do painel:</td>
<td><input name="start_acp" type="text" id="startacp" value="<?php echo $exibe["startacp"]; ?>"></td>
</tr>
<tr>
<td width="100"> </td>
<td> </td>
</tr>
<tr>
<td width="100"> </td>
<td>
<input name="update" type="submit" id="update" value="Salvar">
</td>
</tr>
</table>
</form>

Keep in mind that the connection information is correct if there wasn’t going to be an error, right? Why can’t I see the value inside the respective fields?

1 answer

4


I changed something that is obviously wrong. I don’t know if it will produce the result you expect but at least it shows you where the error is.

You ended the while with a ;. It will not keep repeating this way. This command expects a block of other commands that will be repeated until the condition is false. You have to set this block with keys. Actually when it’s a line you don’t need but every good programmer doesn’t care about it and always uses keys to make it easier to catch errors and avoid future maintenance problems.

You probably thought the line below the while with a echo was being executed repeatedly but is not.

It is possible that there are more errors but I could not locate since I can not test this code.

I hope that the select in the database is returning something and has a field called name, otherwise we have another problem but this time in the database. Actually by the way the HTML code looks I hope it only returns one line. If you return several, the page will be in chaos.

Can I assume that there will only be one line in the database or at least the first one that will be relevant in this case? So I think it makes more sense in what seems to be what you want to do. I’m going to change the code so and then you don’t need the while more. The while it would only be necessary if you wanted to read several lines that I think is not the case.

<?php
// definições de host, database, usuário e senha
$server = "";
$usuario = "";
$banco   = "";
$senha = "";
// conecta ao banco de dados
$conexao = mysql_connect($server, $usuario, $senha);
$conexao = mysql_select_db("$banco",$conexao);
if(!$conexao) {
    echo mysql_error();
    exit;
}

$conexao = mysql_query("select * from settingsMDP");

//********************************************mudei aqui*****************************
$exibe = mysql_fetch_assoc($conexao);
?>

<form method="post" action="<?php $_PHP_SELF ?>">
    <table width="400" border="0" cellspacing="1" cellpadding="2">
        <tr>
            <td width="100">Nome do MDP: </td>
            <td><input name="name" type="text" id="name" value="<?php echo $exibe["name"]; ?>"></td>
        </tr>
        <tr>
            <td width="100">Versão: </td>
            <td><input name="name" type="text" id="name" value="<?php echo $exibe["version"]; ?>"></td>
        </tr>
        <tr>
            <td width="100">Abertura do painel:</td>
            <td><input name="start_acp" type="text" id="startacp" value="<?php echo $exibe["startacp"]; ?>"></td>
        </tr>
        <tr>
            <td width="100"> </td>
            <td> </td>
        </tr>
        <tr>
            <td width="100"> </td>
            <td><input name="update" type="submit" id="update" value="Salvar"></td>
        </tr>
    </table>
</form>

I put in the Github for future reference.

Then you can create validations for the data and you can update the new data in the database with update.

  • Yes friend, good morning! It was your code the solution of the problem. In the most I thank you immensely for your help. : ) It would have to return only one same line. There is no more than that. Anyway, I wanted to know if, using the UPDATE feature I can do the appropriate validations in this form. It is possible?

  • Answering your last comment in the removed answer: If it’s the same problem, you’d better edit your question to add more information. If it is another problem (even if it is in the same code), it is better to post another question, otherwise mischaracterize this. Here it is not a forum, it is a website of questions and answers. That’s why it’s important to keep organized. That is, if you managed to evolve a little and now need to solve another detail of this code, is another question.

  • Okay. So, thank you.

Browser other questions tagged

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