我正在尝试在MySQL数据库中搜索名称。它没有用......有什么建议吗?

时间:2017-05-30 16:06:33

标签: php html mysql database phpmyadmin

我正在为我的php服务器使用000webhost,为我的数据库使用phpmyadmin。我正在尝试这样做,当您输入用户名时,它会输出密码。任何人都可以给我一些建议吗?那太好了!

我的HTML:

<!DOCTYPE html>
<html>
<body>

<form action="Login.php" method="GET">
        <input type="text" name="query" />
        <input type="submit" value="Search" />
    </form>

</body>
</html>

我的php:

<?php

$host='localhost';
$user='id1783920_123456';
$pass='';
$db='id1783920_mydb';

/* $pass is the password and I know that's not the problem, I just don't want to share it */ 

$con=mysqli_connect($host,$user,$pass,$db);
if($con) {
echo 'connected successfully to id1783920_mydb database';
}

$term = $_GET['query'];

$sql = "SELECT password FROM Signup WHERE CONCAT( username) LIKE '%$term%'";
$result = $con->query($sql);

if ($result->num_rows > 0) {    
$row = $result->fetch_assoc() 
echo "password: " . $row["password"];

} else {
echo "0 results";
}
$conn->close();

?>

2 个答案:

答案 0 :(得分:0)

你可以试试这个。

$mysqli = new mysqli($host,$user,$pass,$db);
if ($mysqli->connect_errno) {
  //error
}
$sql = "SELECT password FROM your_table WHERE user = $name";
if (!$result = $mysqli->query($sql)) {
  //something didn't go right   
}
if ($result->num_rows === 0) {
  //no result
}
//you should have the password in here
$password = $result->fetch_assoc();

$result->free();
$mysqli->close();

连接到数据库后,您应检查是否实际建立了连接,如果没有,您会收到哪些错误:

if ($mysqli->connect_errno) {
  var_dump($mysqli->connect_errno);
  var_dump($mysqli->connect_error);
}

您需要在某处编写SQL,无论是在变量中还是直接在查询中。

$sql = "SELECT password FROM your_table WHERE user = $name";

$mysqli->query($sql) - 这将执行查询。如果您的代码最终出现在我提供的if中,那就出错了。因此,您可以在那里设置打印以查看错误。

if (!$result = $mysqli->query($sql)) {
  var_dump($mysqli->connect_errno);
  var_dump($mysqli->connect_error);
}

检查是否有任何结果。

if ($result->num_rows === 0) {
     echo "No results";
}

$result->fetch_assoc(); - 这将“获取”结果。您可以将其设置为变量:

$password = $result->fetch_assoc();

然后不要忘记释放结果并关闭连接:

$result->free();
$mysqli->close();

答案 1 :(得分:0)

您的PHP文件应如下所示:

<?php

$host='localhost';
$user='id1783920_123456';
$pass='';
$db='id1783920_mydb';    

$con=mysqli_connect($host,$user,$pass,$db);
if($con) {
echo 'connected successfully to id1783920_mydb database';
}

$term = $_GET['query'];

$sql = "SELECT password FROM Signup WHERE CONCAT( username) LIKE '%$term%'";
$result = $con->query($sql);

if ($result->num_rows > 0) {    
$row = $result->fetch_assoc(); 
echo "password: " . $row["password"];

} else {
echo "0 results";
}
$conn->close();

?>

快乐的编码!