-2

I have read in another threads that pasting the query in phpMyAdmin returns the amount of rows that you have, but in my case it doesn't even returns a value in phpMyAdmin, it's always 0! Here is the query:

$query = "SELECT nom_usu FROM usuarios WHERE nom_usu = '$usu' AND pass = '$pass';";

I open the .php file and run the query and it returns empty values, like this:

SELECT nom_usu FROM usuarios WHERE nom_usu = '' AND pass = '';

I also tried to echo the value that returns and is, guess what? Zero!

Here is the complete .php file (is for a login):

<?php

    include('dbConfig.php');

    $usu = $_POST["usu"];
    $pass = $_POST["pass"];

    $query = "SELECT nom_usu FROM usuarios WHERE nom_usu = '$usu' AND pass = '$pass';";

    echo $query."\n";

    if($resultado = mysql_query($query)){
      if(mysql_num_rows($resultado) > 0){
         echo mysql_num_rows($resultado);
         echo "Todo est&aacute; bien, no te preocupes :)";
         echo true;
      } else {
         echo mysql_num_rows($resultado);
         echo "Hay algo mal aqu&iacute; :(";
      }
    } else {
     echo false;
    }

?>

And the dbConfig.php file:

<?php

$host="localhost";
$user="neosoftw_lambda";
$contra="myPass"; <- This is not actually my password...
$db="neosoftw_lambdaMovil";

$ms = mysql_connect($host, $user, $contra) or die("No se pudo conectar :(");
mysql_select_db($db, $ms) or die ("No hay base de datos :(");   

?>

Hope someone can help me figure out where I have gone wrong?

Here is what I want to make, is a login in jQuery Mobile, but it doesn't work!

Username: bryan Password: azul

http://android.neosoftware.org.mx/

Edit

Maybe it could be because of my javascript?

$.post("http://android.neosoftware.org.mx/PHP/login.php", {usu : usu, pass : pass}, function(respuesta_login){

            if(!($("#txt_usuario").val() || $("#txt_password") == '')){
                $.mobile.changePage("#campos_vacios");
            } else {
                if(respuesta_login == 1){
                    $("#txt_usuario").val('');
                    $("#txt_password").val('');
                    $.mobile.changePage("#pag_principal");
                } else {
                    $.mobile.changePage("#error_login");
                }
            }


        });
9
  • 2
    Can you post your database structure with data? Also, sql injection .. blah blah blah.
    – Dave Chen
    Nov 4, 2013 at 5:35
  • Your POST values are not there, simple as that. What is the output of var_dump($_POST); ? Nov 4, 2013 at 5:54
  • Are you aware that you cannot ordinarily send values using AJAX to a different domain? android.neosoftware.org.mx/PHP/login.php is that the same domain as your javascript one? Nov 4, 2013 at 6:13
  • Yes, I'm using that domain to send the values in the login form Nov 4, 2013 at 6:16
  • I mean to ask whether your login form also resides on android.neosoftware.org.mx ? and what are the values of your usu and pass javascript variables? Nov 4, 2013 at 6:17

2 Answers 2

0

Use mysqli or PDO statements, as mysql is deprecated. Try this:

 $usu = mysqli_real_escape_string($_POST["usu"]);
 $pass = mysqli_real_escape_string($_POST["pass"]);

 $query = "SELECT nom_usu FROM usuarios WHERE nom_usu = '".$usu."' AND pass = '".$pass."'";

And also your condition is incorrect.It should be like this:

if($resultado == mysqli_query($query)){
3
  • It didn't work, any other idea of what it could be? I don't understand what's wrong with it! The variables are empty in the query, and I'm sending the correct fields and the jQuery application always send me to the "Incorrect information" dialog! Nov 4, 2013 at 5:53
  • Your POST values are not there, simple as that. What is the output of var_dump($_POST);? Nov 4, 2013 at 5:56
  • Yes so that means check your FORM that posts to this PHP, your PHP is fine Nov 4, 2013 at 6:06
0

Try this query.

$query = "SELECT `nom_usu` FROM usuarios WHERE `nom_usu` = '".$usu."' AND `pass` = '".$pass."'";


Also this type of problem occurre when $_POST variable is empty.. So, double check your code.


Note : mysql_* function is deprecated, move on mysqli_* function asap.

10
  • 1
    What is different in this query, functionally, from OP's query except back ticks? how is this supposed to solve it? Nov 4, 2013 at 5:52
  • @Hanky웃Panky did you checked my whole query? you can't see any other update? Nov 4, 2013 at 5:56
  • Nope I cannot see any other update. Those extra quotation marks are not a must and your query will produce 100% same result as their query did. Interpolation works fine in double quotes. Their problem is that those variables do not hold a value. Nov 4, 2013 at 5:58
  • that depends on the structure of code. sometime without double quotes php variable remains same (0). I faced this many times, and sometime it works.. :) Nov 4, 2013 at 6:03
  • 2
    Impossible. if $usu = $_POST["usu"]; doesn't give a value to $usu in double quotes then the only reason is that it was not set in POST. Programming is not guess work, it either works or it doesn't work. This will work 1 million out of 1 million times. There is no depends or sometimes in this case :) Nov 4, 2013 at 6:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.