Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Returning multiple values from a function

$
0
0
Hey again guys,

I'm creating a new login/registration system based on a tutorial from phpacademy.org. But since it's a tutoria;, it's a little on the basic side. So I'm trying to expand it as I go to fit my needs.

For example his passwords use md5 for the sake of simplicity, however I'm using crypt with a randomly generated salt that is stored in the database.

There are two functions that I'm working with for the login system, here they are.

function userid_from_username (){
	$username = sanitize($username);
	$query = mysql_query("SELECT `id`, `salt`
						  FROM `user_data`
						  WHERE`username` = '$username'
						");	
	
	return mysql_result($query, 0, 'id');
}

function login ($username, $password){
	$user_id = userid_from_username($username);
	$salt; //Define once I figure out how to get the value back from the function above
	$username = sanitize($username);
	$password = crypt($password, $salt);
	
	$query = mysql_query("SELECT COUNT(`id`)
						  FROM `user_data`
						  WHERE `username` = '$username'
						  AND `password` = '$password'
						");
	
	return (mysql_result($query, 0) == 1) ? $user_id : false;
}



I suppose it's actually 3 functions. The sanitize function simply runs the username variable through mysql_real_escape_string and returns it.

(I know that the mysql functions are recommended to be replaced by PDO or mysqli, and once I am comfortable with PDO I will, but at the moment I am still having trouble with that.)

So basically what I'm trying to do is make the top function return both the user_id and the salt. I tried using an array and then telling it to return the array but I got an error about mysql_result not being able to jump to 0 or 1.

I also tried using mysql_fetch_assoc and then returning the array from that but when I tried to access the array it said that it was undefined.

Unfortunatly I lost the changes I had made to that point.

Viewing all articles
Browse latest Browse all 51036

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>