Getting Rows from MySQL
Posted: Sat Nov 08, 2008 12:25 am
Hello,
I have a problem with my PHP script. Im trying to test if a row has the same value as a user input.
I want to test and see if $Username is equal to a specific row in the $Usernames Rows.
Result:
EDIT: I fixed it. Here's the code i used:
I have a problem with my PHP script. Im trying to test if a row has the same value as a user input.
I want to test and see if $Username is equal to a specific row in the $Usernames Rows.
Code: Select all
<?php
$Username = $_POST["Username"];
$Password = $_POST["Password"];
$Connected = mysql_connect("127.0.0.1:8889", "Zymus", "carpediemcarpenoctem");
if($Connected)
{
$SelectedDB = mysql_select_db("oraculum");
if(!$SelectedDB)
{
echo mysql_error();
}
$Query = "SELECT Username, Password FROM Information";
$Result = mysql_query($Query);
if($Result)
{
$Information = mysql_fetch_array($Result);
if($Information)
{
echo "Retrieved Result";
echo "<br>";
}
else
{
echo "Failed";
echo "<br>";
}
while($Information)
{
$Usernames = $Result['Username'];
if($Usernames[$Index] == $Username)
{
echo "Successful";
echo "<br>";
break;
}
else
{
echo "$Usernames[$Index]";
}
}
}
else
{
echo mysql_error();
}
}
else
{
echo mysql_error();
}
?>
What am i doing wrong? I want it to test if the row value equals the $Username value, then say successful, and stop. It doesn't test if it's equal or not... Should i be using a for loop, or am i just doing something wrong?Retrieved Result
EDIT: I fixed it. Here's the code i used:
Code: Select all
<?php
$Username = $_POST["Username"];
$Password = $_POST["Password"];
$Connected = mysql_connect("127.0.0.1:8889", "Zymus", "carpediemcarpenoctem");
if($Connected)
{
$SelectedDB = mysql_select_db("oraculum");
if(!$SelectedDB)
{
echo mysql_error();
echo "<br>";
}
$Query = "SELECT `Username`, `Password` FROM `Information`";
$Result = mysql_query($Query);
if($Result)
{
$Information = mysql_fetch_array($Result);
/*if($Information)
{
echo "Retrieved Result";
echo "<br>";
}
else
{
echo mysql_error();
echo "<br>";
}*/
$NumberOfRows = mysql_num_rows($Result);
for($Index = 0; $Index < $NumberOfRows; $Index++)
{
$DBUsername = mysql_result($Result, $Index, "Username");
$DBPassword = mysql_result($Result, $Index, "Password");
if($DBUsername == $Username)
{
if($DBPassword == $Password)
{
echo "Successfully Logged In As: $Username";
echo "<br>";
echo "With Password: $Password";
echo "<br>";
break;
}
else
{
echo "Invalid Password";
echo "<br>";
}
}
else
{
echo mysql_error();
echo "<br>";
}
}
}
else
{
echo mysql_error();
echo "<br>";
}
}
else
{
echo mysql_error();
echo "<br>";
}
?>