当前位置: 动力学知识库 > 问答 > 编程问答 >

javascript - PHP/MySQL Query Issues

问题描述:

I am attempting to set up a basic log in system for a website but I am having major issues getting the query to operate correctly. So far everything I have tried to do to figure out what is going on has failed. As far as I can tell the query itself is not failing but it is not returning any data either. Using the query in phpMyAdmin results in the proper query information being returned.

Here is my php code:

<?PHP

//Empty error variables

$sqlerror;

if(empty($_POST['uname']))

{

$data = "false,Username field empty";

echo json_encode($data);

return false;

}

if (empty($_POST['pword']))

{

$data = "false,Password field empty";

echo json_encode($data);

return false;

}

$username = $_POST['uname'];

$password = $_POST['pword'];

echo $username."<br/>";

echo $password."<br/>";

if(!CheckDB())

{

$data = "false,".$sqlerror;

echo json_encode($data);

return false;

}

else

{

CheckDBL();

}

function CheckDB()

{

echo "Made it to CheckDB! <br/>";

$connection = mysqli_connect("xxxx","xxxx","xxxx","xxxx");

if(mysqli_connect_errno())

{

$sqlerror = "Could not log in to database";

return false;

}

echo "Connection established! <br/>";

mysqli_close($connection);

return true;

}

function CheckDBL($username,$password)

{

echo "Made it to CheckDBL! <br/>";

$sql = mysqli_connect("xxxx","xxxx","xxxx","xxxx");

if(mysqli_connect_errno())

{

echo "Connection failed";

return false;

}

if ($query = mysqli_prepare($sql,"Select Password From login_info Where Username = ?"))

{

mysqli_stmt_bind_param($query, "s", $username);

if (!mysqli_stmt_execute($query))

{

echo "Query failed! <br/>";

echo mysqli_error($sql);

return false;

}

else

{

echo "Query successful <br/>";

}

mysqli_stmt_bind_result($query,$password2);

mysqli_stmt_fetch($query);

echo "The password is: ".$password2;

}

else

{

echo "Statement preparation failed <br/>";

}

}

?>

And the output in my browser from this code:

GWil

TestPassword

Made it to CheckDB!

Connection established!

Made it to CheckDBL!

Query successful

The password is:

网友答案:
$connect = mysqli_connect("localhost","root","","database") or die (mysqli_error($connect));
mysqli_set_charset($connect,"utf8");

$username="hej";

if ($query = mysqli_prepare($connect,"Select Password From login_info Where Username =?")){

mysqli_stmt_bind_param($query, "s", $username);

    if (!mysqli_stmt_execute($query)){

    echo "Query failed! <br/>"; 
    echo mysqli_error($query);
    return false;
    }else{
    echo "Query successful <br/>";
    }
mysqli_stmt_bind_result($query,$password2);<br/>
mysqli_stmt_fetch($query);<br/> echo "The password is: ".$password2;<br/>
}else{
echo "Statement preparation failed";}

in table login_info i have username=hej and password=123

this will achive what you are looking for.

分享给朋友:
您可能感兴趣的文章:
随机阅读: