Retrieve data from the database

Asked

Viewed 74 times

0

Good is the following I have the following code:

<?php
$verifica = mysqli_query("SELECT * from dados WHERE login='$login'");
$array = mysql_fetch_array($verifica);

$array[senha];

?>

Supposedly, I want to get the login data, where login is equal to variable $login, and then display the password of the same, however I appear the following errors:

Warning: mysqli_query() expects at least 2 Parameters, 1 Given in C: Program Files Vertrigoserv www Adm logar.php on line 9

Warning: mysql_fetch_array() expects Parameter 1 to be Resource, null Given in C: Program Files Vertrigoserv www Adm logar.php on line 10

What’s wrong?

Thank you.

  • 1

    Aren’t you forgetting to pass the connection parameter to mysqli? And another, you’re mixing the libraries. If it started with mysqli, it cannot abruptly change to mysql(which is even deprecated).

  • What do you mean pass the? parameter is that I am from the time of mysql_query.

  • 1

    See: http://php.net/manual/en/mysqli.query.php

  • I couldn’t understand.

2 answers

5


The function mysqli_query() in procedural mode, expect a variable with parameters to create a connection with the database and another with the query itself, you are only passing the query.

Try it like this:

$link = mysqli_connect("localhost", "my_user", "my_password", "dbname");

$verifica = mysqli_query($link, "SELECT * from dados WHERE login='$login'");
$array = mysqli_fetch_array($verifica);

$array['senha'];

However, the query would be very vulnerable this way, you could parameterize this query, this way:

$link = mysqli_connect("localhost", "my_user", "my_password", "dbname");
//prepara a query pra receber o parametro ?
$stmt = mysqli_prepare($link, "SELECT senha from dados WHERE login= ?");
//passa o parametro e o tipo("s" quer dizer string)
mysqli_stmt_bind_param($stmt, "s", $login);
//executa a query
mysqli_stmt_execute($stmt);
//vincula o retorno a variavel $verifica
mysqli_stmt_bind_result($stmt, $verifica);
mysqli_stmt_fetch($stmt);
printf($verifica);// exibe a senha
/* fecha o statement */
mysqli_stmt_close($stmt);

This slightly lessens the vulnerability, but it doesn’t mean that the code is safe just with this.

0

It is necessary to pass the connection parameters when using the mysqli_query functions, and as the comrade said, try not to mix the two libraries.

More about the mysqli: http://php.net/manual/en/class.mysqli.php

  • Good evening, I couldn’t quite figure out this mysqli library, how can I apply to my case?

Browser other questions tagged

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