PHP return data from the database

1

I am trying to make a select in the database for testing, this select displays the user data after entering username and password. The error happens after select :

  

Warning: mysql_fetch_array () expects parameter 1 to be resource,   integer given in C: \ xampp \ htdocs \ PHP1 \ query.php on line 57

<? php
if (@$_GET['go'] == 'consultar') {
  $user = $_POST['usuario'];
  $pwd = $_POST['senha'];

  if (empty($user)) {
    echo "<script>alert('Preencha todos os campos para logar-se.'); history.back();</script>";
  }
  elseif(empty($pwd)) {
    echo "<script>alert('Preencha todos os campos para logar-se.'); </script>";
  } else {
    $query1 = mysql_num_rows(mysql_query("SELECT * FROM USUARIO WHERE USUARIO = '$user' AND SENHA = '$pwd'"));
    if ($query1 == 1) {
      echo "<table><tr><td>Login</td><td>Nome do Usuário</td><td>Senha do Usuário</td></tr>";
      while ($escrever = mysql_fetch_array($query1)) {

        /*Escreve cada linha da tabela*/
        echo "<tr><td>".$escrever['usuario'].
        "</td><td>".$escrever['nome'].
        "</td><td>".$escrever['senha'].
        "</td></tr>";

      }
      echo "</table>";


    } else {
      echo "<script>alert('Usuário ou senha não correspondem!');</script>";


    }
  }

} ?>
<?php require_once "config.php"; ?>

<html>

<head>
  <meta charset=utf-8>
  <meta name=description content="">
  <meta name=viewport content="width=device-width, initial-scale=1">
  <link rel="stylesheet" type="text/css" href="css/style.css">
  <title>Cadastro com PHP</title>
</head>

<body>
  <div id="cadastro">
    <form method="post" action="?go=consultar">
      <table id="login_table">

        <tr>
          <td>Usuário:</td>
          <td>
            <input type="text" name="usuario" id="usuario" class="txt" maxlength="15" />
          </td>
        </tr>
        <tr>
          <td>Senha:</td>
          <td>
            <input type="password" name="senha" id="senha" class="txt" maxlength="15" />
          </td>
        </tr>
        <tr>
          <td>Nome:</td>
          <td>
            <input type="text" name="nome" id="nome" class="txt" />
          </td>
        </tr>
        <tr>
          <td>Email:</td>
          <td>
            <input type="text" name="email" id="email" class="txt" />
          </td>
        </tr>
        <tr>
          <td colspan="2">
            <input type="submit" value="Consultar" id="btn">
        </tr>

      </table>
    </form>
  </div>
</body>

</html>
    
asked by anonymous 27.03.2015 / 12:11

1 answer

1

While you need to pass the query result ( resource ) and not the number of rows returned by the query, create a variable for this:

$query1 = mysql_num_rows(mysql_query("SELECT * FROM USUARIO ..."));

//código omitido ...

while ($escrever = mysql_fetch_array($query1)) {

Note that $query will not receive the return of mysql_query() , but of mysql_num_rows() which is an integer. Avoid chaining too many instructions that make it difficult to detect errors, when writing your code remember to use mysql_error () to display the bank's error messages.

You can correct it this way:

$sql = "SELECT * FROM USUARIO WHERE USUARIO = '$user' AND SENHA = '$pwd'"
$query1 = mysql_query($sql) or die(mysql_error());
$total_registros = mysql_num_rows($query1);
//código omitido ...
while ($escrever = mysql_fetch_array($query1)) {

If it is a new project, consider changing the mysql_ * functions by PDO or mysqli.

Recommended reading:

Why should not we use functions of type mysql_ *?

MySQL vs PDO - Which is the most recommended to use?

How to prevent SQL injection in my PHP code

    
27.03.2015 / 12:20