Reputation: 1283
I trying to call my method like this:
<?php getalluser($catid);?>
And there is a select query in my getalluser() method:
function getalluser($query)
{
$sql = "SELECT * from user where catId='".$query."'";
}
In another page, I call the same method and pass user id instead of category id like so:
<?php getalluser($userID);?>
Now, how i can identify if there is a category id or a user id in $query, so I can change in my where condition base on my $query.
Upvotes: 0
Views: 63
Reputation:
If you really wished, you could do this
function getUserById($id, $searctype='catId') {
// Use this to prevent anything other than userId and catId from being entered
$types = array("catId", "userId");
$searchtype = (in_array($searctype, $types) ? $searchtype:"catId";
$sql = "SELECT * from user where ".$searchtype."='".$id."'";
}
then call `getUserById($userId, "userId");
Note: Please look into using PDO. It's a safer way to deal with SQL & helps prevent SQL injection.
Upvotes: 0
Reputation: 582
"Both are integers" You can maybe follow kfirba hint or you can add another param defaulted to false
function getalluser($query, $user = "")
{
if($user == ''){
$sql = "SELECT * from user where catId='".$query."'";
} else {
// Query for users
}
}
Cat call:
<?php getalluser($catid);?>
User call:
<?php getalluser('', $userid);?> // example with first param empty
Upvotes: 0
Reputation: 23978
Add another parameter, called from.
Options should be:
Users
Categories
function getalluser($query, $calledFrom = '')
{
if ($calledFrom == 'users') {
$sql = "SELECT * from user where catId='".$query."'";
}
if ($calledFrom == 'categories') {
// SQL for fetching categories.
}
}
And call the function like:
<?php getalluser($catid, 'users');?>
<?php getalluser($catid, 'categories');?>
Upvotes: 2
Reputation: 672
You can use debug_backtrace function. With this information you'll know wich file/function called it.
Upvotes: 0