I’m new to mySQL and I’ve been stumbling around trying to get my bearings. When I want to get results from a table, I’ve been using a while loop to go through the query results. What can I do if I’m only expecting one result? I don’t want to loop. If there’s a result, I want to populate the $return value with the xml data like I’m doing in the loop. If there’s no result, I want to send an alternative $return value to indicate no record exists.
How can I do an if/else instead of a while loop?
Here’s the code:
$query = "SELECT * FROM Users WHERE username = '$username' AND password='$password'";
$result = mysql_query ($query);
while($line = mysql_fetch_assoc($result)){
$return = "<user download='pass'>
";
$return .= "<username>".$line["username"]."</username>
";
$return .= "<password>".$line["password"]."</password>
";
$return .= "<firstName>".$line["firstName"]."</firstName>
";
$return .= "<lastName>".$line["lastName"]."</lastName>
";
$return .= "<address1>".$line["address1"]."</address1>
";
$return .= "<address2>".$line["address2"]."</address2>
";
$return .= "<city>".$line["city"]."</city>
";
$return .= "<zip>".$line["zip"]."</zip>
";
$return .= "<state>".$line["state"]."</state>
";
$return .= "<country>".$line["country"]."</country>
";
$return .= "<company>".$line["company"]."</company>
";
$return .= "</user>";
}