1

I have seen a case where for example a URL like this http://example.com/?edit=39 straight away opens a page where you can edit only the article/post which has ID = 39. I have read a lot about calling a function via URL but I have never met the case where you can catch parameter from the URL, pass the parameter into the function and then execute the function.

Using URL http://example.com/?edit=39 as example, how do I use a function to identify 39 as the ID and retrieve a post from the database which has ID = 39.

1
  • $id=$_REQUEST['edit']; Commented Nov 27, 2017 at 6:22

2 Answers 2

1

For example your URL is test.php?edit=39

if (isset($_GET['edit'])) {// it will catch the 39
    $id= $_GET['edit'];
     $return_value=funciton_name($id);// passing parameter to the function
     echo $return_value;// it will display the return vlaue.
}

//execute that function and return the value

function funciton_name($id){
    $result1 = "SELECT * FROM tablename WHERE id=".$id;
    return $result1;
}
Sign up to request clarification or add additional context in comments.

1 Comment

@Narenda, thank you for this. You got me on the right path
1

Use parse_url() and parse_str() functions.

For example

$parts = parse_url($url); parse_str($parts['query'], $query); echo $query['email'];

If you want to get the $url dynamically have a look at this below question: Get the full URL in PHP

2 Comments

Thank @seravee, however I did not understand $query['email']. where from the ['email']?
@Elizabeth Kof email is an argument if suppose it contains in the arguments i given as a sample format. for your URL it should be edit.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.