Reputation: 113
I've run into a strange issue. I'm developing a web interface for a MySQL database, and trying to fetch information from it using PHP. In short, I'm able to retrieve information just fine from some databases while I am not with others.
$userList = mysql_query("SELECT * FROM myTable");
while ($userInfo = mysql_fetch_array($userList) )
{
echo "<p>Name = " . $userInfo["name"] . ". Password = " . $userInfo["password"] . ".</p>";
}
That part is just a test, and it works fine. I get the name and password of everyone in the database. But when I try to do it this way, I run into errors. Consider that
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>";
}
To be specific, the $userInfo["name"] and $userInfo["password"] characters return absolutely nothing in the second block of code, while in the first block of code they seem to work just fine. I don't understand why there is a difference between the two.
Any help or advice I could receive would be greatly appreciated.
EDIT: For those who want the full code, here it is.
<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);
}
?>
EDIT #2: Some people have noted that this presentation is very insecure with passwords, and I would agree - this isn't intended to be the final code of the website at all. I just thought I'd test the connection as I went and ran into this issue.
Upvotes: 1
Views: 91
Reputation: 26
Once you have finished the first block the cursor is at the end of the $userList. So in the second block there is nothing more to read with
$userInfo = mysql_fetch_array($userList)
Try to include the following statement before the second block, to move the cursor back to the first item:
mysql_data_seek($userList, 0);
or better:
if (!mysql_data_seek($userList, 0))
{
echo "You can't go back (seek): " . mysql_error() . "\n";
}
else
{
// Block 2
}
Upvotes: 1
Reputation: 162
It is because of (!($found)
in your while loop. the final state of that must be true for the loop to take action however it comes out false all the time because you are asking it to be not false but it is. There fore it will not work because the conditions are not met.
Try removing the !
from it and test it. If you cannot do it this way try using this:
do {
//your code here to motion
}
while()// your conditions
in this case as long as the states are true in while
, do
will keep working.
Upvotes: 0
Reputation: 1
I think you show us not the full code. Somewhere you have to set the credentials for the database, is this before or after you put an value to $username? Perhaps you use the $username for the database connection too?
Upvotes: 0