Error in consulting the database! from PHP to MYSQL

1

I had a problem doing this query to the data base. The query variable ($ result1) says 'Error in the query database'.

<?php     
$con=mysqli_connect ('127.0.0.1','root','','freatico') or die ('Error en la conexion');        
$username=$_POST['nombre'];  
$password=$_POST['passa'];  
$sql ="SELECT * FROM 'usuarios' WHERE 'Username'=\'$username\' and 'Password'=\'$password\'";  
$resultado1=mysqli_query($con,$sql) or die ('Error en el query database');
mysqli_close ($con);  
echo 'Si esta registrado';   
?>  
    
asked by Ângel Dâvíd Bermëo 19.05.2018 в 18:16
source

3 answers

2

Write your query like this simply:

<?php     
$con=mysqli_connect ('127.0.0.1','root','','freatico') or die ('Error en la conexion');        
$username=$_POST['nombre'];  
$password=$_POST['passa'];  

/*Modificamos la consulta porque solo interesa saber si hay datos, por eso ponemos COUNT*/
$sql ="SELECT COUNT(*) FROM 'usuarios' WHERE 'Username'='$username' and 'Password'='$password'";  
$resultado1=mysqli_query($con,$sql) or die ('Error en el query database' .mysqli_error($con));
/*Verificamos si encontró registros*/
if (mysqli_num_rows($resultado1) > 0 ){
    /*Liberamos recursos*/
    mysqli_free_result($resultado1);
    mysqli_close ($con);  
    /*
       *Aquí rediriges, poniendo la url que quieres en lugar de lo que hay en http://www.example.com/
       *OJO: cuando se usa header no debe salir nada por pantalla antes de él
       *en el flujo de código que le corresponde
    */

    header('Location: http://www.example.com/');

}else{

      echo 'NO esta registrado';
}


?>  
  

NOTE ON SECURITY OF THE CODE: The query used in this code is highly vulnerable to SQL injection attacks. It was not wanted   deepen the topic because it was far away from the problem   original proposal and also because the OP has stated that it has   Little knowledge about PHP. However I leave this note to warn   about the serious risk of executing queries that concatenate values   coming from abroad. The OP is also recommended to learn in   as much as possible the use of prepared queries as a solution   effective at the risk indicated above.

    
answered by 19.05.2018 / 18:44
source
1

I hope that you help brother, a simple example for your select that you are doing with php mysql.

Likewise, check the official page of php mysqli.

link

<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

$sql = "SELECT id, firstname, lastname FROM MyGuests";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>";
    }
} else {
    echo "0 results";
}
$conn->close();
?>
    
answered by 19.05.2018 в 18:42
1

A simple way to make a query and modern is with the method prepare statments, it is easier and easier. if you're interested in connecting like that;

<?php
$host = "localhost";
$db = "myDbname";
$conexion = new PDO("mysql:host=$host;dbname=$db", 'root', '');
$conexion = $conexion->prepare("SELECT COUNT(*) FROM tabla WHERE id = 2 AND nombre = 'jose'");
$conexion->execute();
?>
    
answered by 20.05.2018 в 01:24