0

I am fetching data from my sql it gives the error here is the my code

    <?php
    $con =     mysql_connect("productivo.db.6420177.hostedresource.com","","");
    if (!$con)
    {


    die('Could not connect: ' . mysql_error());
    }

    mysql_select_db("productivo", $con);

    $username=$_POST['UserName'];
    $password=$_POST['UserPassword'];
    echo($u);
    echo($pw);


     $query = "SELECT *  from  UserCredentials  WHERE UserName='$username' AND  UserPassword='$password'";

//$con = mysql_query($query,$con); //$cnt = mysql_num_rows($con);

  $res = mysql_query($query,$con);
  $cnt  = mysql_num_rows($res);

  echo($cnt);
  if($cnt ==0) { echo "Login Failed"; } else { echo "Yes Successful Login" ; }  ?>

here is the warning it shows

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/content/i/h/u/ihus235/html/cs/pah_brd_v1/productivo/CheckUserCredentialsAPI.php on line 21 Login Failed

you can accees using this url

http://celeritas-solutions.com/pah_brd_v1/productivo/CheckUserCredentialsAPI.html

1
  • 1
    MySQL (mysql_ functions) extension is *deprecated. I suggest to use MySQLi (mysqli*_ functions) or PDO instead. Commented Apr 30, 2013 at 5:40

6 Answers 6

5

The problem is you are trying to use connection variable again don't reuse the same variable again use some other variable

$res = mysql_query($query,$con);
$cnt  = mysql_num_rows($res);
Sign up to request clarification or add additional context in comments.

4 Comments

Why we should now reuse it. Anyhow we are not going to use that connection after that know? please explain.
@Vinoth Babu , there might be no difference, but it's a better snippet for OP. And, yeah, connections should be manually closed, as for me.
You are making a connection instance variable and then try to set by it with query result variable. It is very normal case for application misbehaviour. Also reusing variable is not a better idea every time.
edit your updated code in question. Lets double check what have you changed.
0

you can check error with below code what actually you are doing wrong

 $check = mysql_query("SELECT *  from  UserCredentials  WHERE UserName='$username' AND       UserPassword='$password'")or die(mysql_error());

Comments

0
<?php
     $link = mysqli_connect("productivo.db.6420177.hostedresource.com","","");
     $query = "SELECT *  from  UserCredentials  WHERE UserName='$username' AND       UserPassword='$password'";

     $result = mysqli_query($link,$query);
     $cnt  = mysqli_num_rows($result);


    ?>

Comments

0

Here you use

$con =    mysql_connect("productivo.db.6420177.hostedresource.com","","");

and then for the resultset you use

$con = mysql_query($query,$con);
$cnt  = mysql_num_rows($con);

again. Use $condom for the second.

$condom = mysql_query($query,$con);
$cnt  = mysql_num_rows($condom);

Comments

0

Here u try with this

<?php
    $con =    mysql_connect("productivo.db.6420177.hostedresource.com","","");
   if (!$con)
    {
    die('Could not connect: ' . mysql_error());
   }
    mysql_select_db("productivo", $con);
   $username=$_POST['UserName'];
   $password=$_POST['UserPassword'];
   $result =mysql_query("SELECT *  from  UserCredentials WHERE UserName='$username' AND UserPassword='$password'",$con) or die(mysql_error());
   $cnt  = mysql_num_rows($result);
   if($cnt ==0) { echo "Login Failed"; } else { echo "Yes Successful Login" ; }  ?>

Comments

0

Try

$query = "SELECT *  from  UserCredentials  WHERE UserName='$username' AND UserPassword='$password'";
echo "query= " . $query;

to see what your resulting query looks like. Most probably there is an error in your query that you didn't spot. Best thing is to copy the echo'd result and run it straight on your database.

After that you better use:

$res = mysql_query($query,$con) or die('$query gave error: ' . mysql_error());

This will give you detailed error information from MySQL too.

And last but not least: don't use the mysql_xxx functions anymore, they're deprecated, and very unsafe. This means that your app might not work anymore after a future php-upgrade.

3 Comments

this is the result query= SELECT * from UserCredentials WHERE UserName='jamshaid.ali' AND UserPassword='aliraza40'$query gave error: Table 'productivo.UserCredentials' doesn't exist
Well, that says it all doesn't it :-) Or your table really doesn't exist, or you miss spelled it. If all that is correct, make sure you're connected to the correct database.
i am copying name from the data base it is same it also shows record in admin panel

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.