简体   繁体   中英

I have a PHP code which gives and error when executing. Can anyone point me out the error please.

This is my code.

<?php
require "conn.php";
$user_name = "un";
$user_pass="123";  
$mysql_qry ="select * from user where username like '$user_name' and main_password '$user_pass';";
$result = mysql_query($mysql_qry, $conn);

if(mysql_num_rows($result)>0){
    echo "Login Success!";
}else{
    echo "Invalid Username or Password!";
}

?>

Error message is attached herewith. enter image description here

Please check your $result if it returns false and check your query error.

$result=mysql_query($sql);
if ($result==false)
{
    die(mysql_error());
}
else
{
    echo "Login Access";
}
  1. Please check if you are able to connect to database or not (database name, username and password to connect to database is correct or not)
  2. Check if table name user exists

Do share contents of conn.php if you are unable to resolve the issue

On a separate note

  1. please use mysqli instead of mysql as it is deprecated
  2. modify your query to: $mysql_qry ="select * from user where username = '$user_name' and main_password '$user_pass';"; . like is a very expensive operation

I think something wrong with your MySQL query function, try using

$result = mysqli_query($conn, $mysql_qry);

or

$result = mysql_query($mysql_qry);

instead of

$result = mysql_query($mysql_qry, $conn);

I am wondering how this point missed.. there is an error in your sql query. There is = or like operator missing after main_password. I would not recommend to check password in your query, you should fetch record with username and check password in if condition.

$mysql_qry ="select * from user where username like '$user_name' and main_password = '$user_pass'";

Hope this helps​.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM