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);
}
?>

第二版:有些人指出,此演示文稿的密码非常不安全,我同意这一点——这根本不是网站的最终代码。我只是想在遇到这个问题时测试一下连接。

完成第一个块后,光标位于$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)。它的最终状态必须是真的,循环才能采取行动,但它一直是假的,因为你要求它不是假的,但它确实是。因此,它不会起作用,因为条件不满足。试着从中删除!并测试它。如果你不能这样做,试着使用这个:

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

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