Having trouble with function

The code below is what I have on a page that is giving feedback to the sites administrator to see if people have a username and password, as we give them out after they have registered.


<?

$query  = "SELECT * FROM Register Order by RegId desc";
$result = mysql_query($query);

// Function to read if a client has a Username & Password
function myUserPass(){
$queryPass = "SELECT Username, Password, RegId FROM Register";
$rapid = mysql_query($queryPass);
$rapid['Password'] = Horse;
	
if ($rapid['Password'] == "Horse" ){
	echo "2";
	echo $rapid['Password'];
	} else {
	echo "1";
	}
}

while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{

// Query the database and get the count
$resultCount = mysql_query("SELECT * FROM logdetails Where Log_Id={$row['RegId']}");
$num_rows = mysql_num_rows($resultCount);
// End of query

echo "<div style='position:relative; color:#000000; border-top:#666666 solid 1px; width:700px; height:257px'>".
"<div style='position:relative; float:left; width:450px; height:257px; border-right:#666666 solid 1px;'><br>First Name: {$row['FirstName']} <br>" .
"Last Name: {$row['LastName']} <br>" .
"Company: {$row['Company']} <br>" .
"Address: {$row['Address']} <br>" .
"PostCode: {$row['PostCode']} <br>" .
"Country: {$row['Country']} <br>" .
"Email: <a href=mailto:{$row['Email']}]?subject=TourCheck>{$row['Email']} </a><br>" .
"Tel: {$row['Tel']} <br>" .
"Fax: {$row['Fax']} <br><br>".
"Click <i><a href='#'>here</a></i> to view log in records. <span style='font-size:11px; color:#ff9000'>[$num_rows]</span><br><br>".
"<i><a style='font-size:11px; color:#ff9000' href='logIn.php#topList'>Return To Top [^]</a></i><br><br></div>".
"<div style='position:relative; width:249px; height:257px; float:right; background-color:#cccccc; font-size:12px;'><p style='padding-left:10px; padding-top:6px'>{$row['FirstName']} has logged in <span style='color:#ff0000;'>[$num_rows]</span> times.<br/><br/>Username: [<img width=11px src='../site_images/Tick.jpg' />] <br/><br/>Password: [ " . myUserPass() . " ] </p></div></div>";
}
?>

Where I’m having trouble is that last line calling a function which id does, but I’m not getting the result I wont.

the myUserPass function is looking at the username pad password and then giving a 1 or 0 as to whether a password exists, but I cant get that bit working.

I know there a lot there, but I thought it best to put the lot rather than picking it out.

I deally what I would like is:


function myUserPass(){
$queryPass = "SELECT Username, Password, RegId FROM Register";
$rapid = mysql_query($queryPass);
	
if ($rapid['Password'] == "" ){
	echo "2";
	echo $rapid['Password'];
	} else {
	echo "1";
	}
}

Okay. You’ve got a several issues here.

  1. Your function needs to know the RegId to look someone up. (Pass it as a parameter)
  2. Your query needs to look for that specific individual (WHERE clause)
  3. The line beginning “$rapid =” makes rapid a result set. Extract the row from the result set ([FPHP]mysql_fetch_row[/FPHP] or [FPHP]mysql_fetch_array[/FPHP])
    3b. If it has no rows, you’ve got bigger problems, cause your script is feeding it bad RegId’s.
  4. Once you’ve got the row extracted, you can check it for a password.
    4a. If it doesnt have one, you might want to insert one into the database (another query)
  5. [FPHP]Return[/FPHP] the password instead of [FPHP]echo[/FPHP]'ing it.

If you are only to show whether password exists for that user or not then ‘ternary operator’ will enough for you.


$query  = "SELECT * FROM Register Order by RegId desc"; 
$result = mysql_query($query) or die(mysql_error()); 

while($row = mysql_fetch_array($result, MYSQL_ASSOC)) 
{ 
	// Query the database and get the count     
	$resultCount = mysql_query("SELECT * FROM logdetails Where Log_Id={$row['RegId']}");  
	$num_rows = mysql_num_rows($resultCount); 
	// End of query 
	
	echo "<div style='position:relative; color:#000000; border-top:#666666 solid 1px; width:700px; height:257px'>". 
	"<div style='position:relative; float:left; width:450px; height:257px; border-right:#666666 solid 1px;'><br>First Name: {$row['FirstName']} <br>" . 
	"Last Name: {$row['LastName']} <br>" . 
	"Company: {$row['Company']} <br>" . 
	"Address: {$row['Address']} <br>" . 
	"PostCode: {$row['PostCode']} <br>" . 
	"Country: {$row['Country']} <br>" . 
	"Email: <a href=mailto:{$row['Email']}]?subject=TourCheck>{$row['Email']} </a><br>" . 
	"Tel: {$row['Tel']} <br>" .  
	"Fax: {$row['Fax']} <br><br>". 
	"Click <i><a href='#'>here</a></i> to view log in records. <span style='font-size:11px; color:#ff9000'>[$num_rows]</span><br><br>". 
	"<i><a style='font-size:11px; color:#ff9000' href='logIn.php#topList'>Return To Top [^]</a></i><br><br></div>". 
	"<div style='position:relative; width:249px; height:257px; float:right; background-color:#cccccc; font-size:12px;'><p style='padding-left:10px; padding-top:6px'>{$row['FirstName']} has logged in <span style='color:#ff0000;'>[$num_rows]</span> times.<br/><br/>Username: [<img width=11px src='../site_images/Tick.jpg' />] <br/><br/>Password: [ " . ((isset($row['Password']) && !empty($row['Password'])) ? $row['Password'] : 'No password exists') . " ] </p></div></div>"; 
}

See the following:


((isset($row['Password']) && !empty($row['Password'])) ? $row['Password'] : 'No password exists')

You can change the returning value. I have echoed the password if it exists and simply ‘No password exists’ if not.

Hope you understand what I mean!

OK thank you guy, will have a look through your work and get back to you.

I will say though having read the first reponse Im a little bit lost in the jargon, but will see if i can understand it

WOW thats awsome, thank you very much guys, especially Raju as it worked perfectly…

I can see where I was going wrong now too, so thank you again.