Reputation: 6551
I am creating a Login form that captures the values so I can use throughout the session when the user gets pass the regular Login page. I have input in MySQL as follows:
user_id | username | password | firstname | lastname | email | website | active | date_added
I created a function to capture data but it doesn't return anything. Can anyone help me with this?
Here is what the code looks like:
function user_input($user_id) {
$input = array();
$user_id = (int) $user_id;
$func_num_args = func_num_args();
$func_get_args = func_get_args();
if ($func_num_args > 1) {
unset($func_get_args[0]);
$fields = ' ` ' . implode('`, ` ', $func_get_args) . ' `';
$query = mysql_query("SELECT '$fields' FROM Login WHERE user_id = '$user_id' ");
$input = mysql_fetch_assoc($query);
print_r($input);
}
}
Upvotes: 0
Views: 101
Reputation:
private function user_input($user_id) {
private $input = array();
//$user_id = (int) $user_id; //This line may effect your security
if(isset($input)){
$func_num_args = func_num_args();
$func_get_args = func_get_args();
$query = "SELECT ";
for($i = 0 ; $i <= $func_num_args; $i++){
if($i==($func_num_args-1))
{
$query =$query .$func_get_args[$i];
}
else
{
$query =$query .$func_get_args[$i].",";
}
}
$query = $query . " FROM Login WHERE `user_id` =" . $user_id ;
$rs = mysql_query($query);
while ($getRow = Mysql_fetch_array($rs)){
self::$input = $getRow;
}
}
return self::$input ; // this will be returning an array of result set
}
Upvotes: 2
Reputation: 8970
Change this -
$query = mysql_query("SELECT $fields FROM Login WHERE `user_id` = $user_id ");
Since a variable inside the double quotes echoes it. If you are using a string in where
clause then you have to make it like this '".$xyz['name']."'
. If its a simple integer like user_id
, you can avoid writing in double or single quotes.`
Upvotes: 0
Reputation: 24935
$fields = ' ` ' . implode('`, ` ', $func_get_args) . ' `';
this then makes you fields
` one `, ` two `
This should be
$fields = '`' . implode('`, `', $func_get_args) . '`';
ALSO
You do not need to put ''
around this
'`one`, `two`' < -WRONG
This is correct:
$query = mysql_query("SELECT $fields FROM Login WHERE user_id = '$user_id' ");
Also LEARN PDO!!!
Upvotes: 1