gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 00:23:36 25 4
gpt4 key购买 nike

我遇到了一个奇怪的问题。我正在为 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: 有些人注意到此演示文稿使用密码非常不安全,我同意 - 这根本不是网站的最终代码。我只是想在遇到这个问题时测试连接。

最佳答案

完成第一个 block 后,光标位于 $userList 的末尾。所以在第二个 block 中没有什么可以阅读的了

$userInfo = mysql_fetch_array($userList)  

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

mysql_data_seek($userList, 0);  

或更好:

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

关于php - MySQL 数据库信息未出现在 PHP 页面上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20100046/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com