繁体   English   中英

MySQL数据库信息未出现在PHP页面上

[英]MySQL database information not appearing on a PHP page

我遇到了一个奇怪的问题。 我正在为MySQL数据库开发Web界面,并尝试使用PHP从数据库中获取信息。 简而言之,我可以从某些数据库中检索信息,而我却不在其他数据库中。

$userList = mysql_query("SELECT * FROM myTable");
while ($userInfo = mysql_fetch_array($userList) )
{
    echo "<p>Name = " . $userInfo["name"] . ". Password = " . $userInfo["password"] . ".</p>";
}

那部分只是一个测试,并且工作正常。 我得到数据库中每个人的名称和密码。 但是,当我尝试以这种方式进行操作时,我遇到了错误。 考虑一下

while ( ($userInfo = mysql_fetch_array($userList) ) && (!$found) )
{
    echo "The current username is " . $userInfo["name"] . ". <ul>";
    if ($username == $userInfo["name"])
    {
        $found = true;
        if ($password == $userInfo['password'])
        {
            echo "The password you entered, " . $userInfo['password'] . " was correct!";

            //things which we do once the account is confirmed
        }
        else //the username is right but the password is wrong.
        {
            "The password didn't match, though!";
        }
    }
    else //this username isn't the right one.
    {
        echo "<p>$username does not match " . $userInfo['name'] . ".";
    }
    echo "</ul>";
}

具体来说,$ userInfo [“ name”]和$ userInfo [“ password”]字符在第二个代码块中完全不返回任何内容,而在第一个代码块中,它们似乎可以正常工作。 我不明白为什么两者之间会有区别。

我能收到的任何帮助或建议将不胜感激。

编辑:对于那些想要完整的代码,这是。

<head>
<title>My Web Interface</title>
</head>

<?php
if (!($_POST["go"]))
{
?><h1>Hello World!</h1>
<form action="test.php" method="post">
    Username: <input type="text" name="username" /> <br />
Password: <input type="text" name="password" /> <br />
<input type="submit" name="go" />
</form>
<?php
}
else //the user has submitted: in which case, we check if the details match any user info in the database
{

$username = $_POST["username"];
$password = $_POST["password"];

//the database info variables
$hostname = "myhostname";
$dbUsername = "myusername";
$dbPassword = "mypassword";

echo "<p>You entered the username and password combo of '$username' and '$password'.</p>";

$connect = mysql_connect($hostname, $dbUsername, $dbPassword) or die ("Unable to connect to MySQL");

//test for the connection's presence. Every time so far it's returned True.
if ($connect)
{
    echo "Got it!";
}
else
{
    echo "Don't got it!";
}

//echo "<p>My username is " . $dbUsername ", my hostname is " . $hostname . " and my password is " . $dbPassword . ".</p>";

$selected = mysql_select_db("myDatabase",$connect)
or die("Could not select examples");

$userList = mysql_query("SELECT * FROM testUsers;");

/****
* This part tests to show a connection between the user and the database.
* It should return a list of users and the rights they have.
***
*/
$found = false; //how we terminate the loop.

//echo "<ul>";
while ($userInfo = mysql_fetch_array($userList) )
{
    echo "<p>Name = " . $userInfo["name"] . ". Password = " . $userInfo["password"] . ".</p>";
    if ($userInfo["password"] == $password)
    {
        echo "<p>The passwords match and are both $password!</p>";
    }
    else
    {
        echo "<p>$password does not match with " . $userInfo["password"] . "!</p>";
    }
}

while ( ($userInfo = mysql_fetch_array($userList) ) && (!($found)) )
{
    echo "The current username is " . $userInfo["name"] . ". <ul>";
    if ($username == $userInfo["name"])
    {
        $found = true;
        echo "<p>We found you in the database, " . $userInfo['name'] . ". Now to test your password.</p>";
        if ($password == $userInfo['password'])
        {
            echo "<p>The password you entered, " . $userInfo['password'] . " was correct!</p>";
            //now show the table's contents
            $register = mysql_query("SELECT * FROM myTable;");
            while ($col = mysql_fetch_array($register) )
            {
                echo "<li>Tag: " . $col['service_tag'] . "</li>";
            }
        }
        else //the username is right but the password is wrong.
        {
            echo "The password didn't match, though!";
        }
    }
    else //this username isn't the right one.
    {
        echo "<p>$username does not match " . $userInfo['name'] . ".";
    }
    echo "</ul>";
}

/*
*Test code: trying to output the testUsers info without the conditions.
*/

if (!$found)
{
    echo "<p>We could not find you in the database. Did you enter your username correctly?</p>";
}
echo "</ul>"; 

mysql_close($connect);
}
?>

编辑#2:有人指出,此演示文稿使用密码非常不安全,我同意-这根本不打算成为网站的最终代码。 我只是以为自己会在测试并尝试连接时遇到此问题。

完成第一个块后,光标将在$ userList的末尾。 因此,在第二个块中,没有其他内容可供阅读

$userInfo = mysql_fetch_array($userList)  

尝试在第二个块之前包含以下语句,以将光标移回第一项:

mysql_data_seek($userList, 0);  

或更好:

if (!mysql_data_seek($userList, 0))   
{  
    echo "You can't go back (seek): " . mysql_error() . "\n";  
}   
else  
{  
   // Block 2  
}  

我认为您没有向我们展示完整的代码。 您必须在某个位置设置数据库的凭据,这是在将值设置为$ username之前还是之后? 也许您也将$ username用于数据库连接?

这是因为while循环中的(!($found) 。循环的最终状态必须为true才能采取操作,但是由于您要求它不是false,而是始终都是false。有可能因为不满足条件而无法使用,请尝试从中删除!并进行测试,如果无法这样做,请尝试使用以下方法:

do {
//your code here to motion
}
while()// your conditions

在这种情况下,只要while的状态为true, do就会继续工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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