PHP return database data

Asked

Viewed 1,124 times

1

I’m trying to make a select in the test database, select displays user data after entering user and password. The error happens after the 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>

1 answer

2


No 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() and yes of mysql_num_rows() which is an integer. Avoid chaining too many instructions this makes it difficult to detect errors, when writing your code remember to use mysql_error() to display bank error messages.

You can fix it that 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 you are a new project, consider changing the mysql_* functions by PDO or mysqli.

recommended reading:

Why should we not use mysql type functions_*?

Mysqli vs PDO - which is the most recommended to use?

How to prevent SQL code injection into my PHP code

  • It worked, thank you.

Browser other questions tagged

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