0

I am new to PHP. I need a help regarding the methods of extracting DB name and table name from the given URL name. For example, let's say, I have an URL like the one below:

/test.php?db=...&table=.../

How to extract the DB name and table name from this URL using PHP and use the result for other query purposes.

2

2 Answers 2

2

If you mean how to parse an existing URL for it's parameters:

parse_url() and parse_str() will help you strip the components of the url. You will primarily be looking at the following

$elements = parse_url($url);
$kvps = $elements->query;
$db = parse_str($kvps['db']);
$table = parse_str($kvps['table']);

But, if you mean how to GET variables from the current page before render:

<?php

$dbname = $_GET['db'];
$tablename = $_GET['table'];

?>

And yea, there are major security risks involved in opening up 'direct' access to your database this way. Best to obfuscate / encapsulate / wrap your functions in tasks like index.php&addUser=tim instead of index.php&insert=tim&db=boofar&table=users&dbuser=root&dbpassword=secure.

If you're just learning, what you're doing is fine, as long as you realize why it's wrong. If you're coding for production, you really need an alternate solution.

Sign up to request clarification or add additional context in comments.

4 Comments

Similar to that..but I have to get the values from URL.How to do that?
Use parse_url() as @Pekka has suggested.
$_GET are the values from the URL query string.
@Geoist feel free to incorporate parse_url() and parse_str() into your answer... people too lazy to answer, adding a comment instead, forfeit their rights :)
1

There are two ways to pass variables or data to another page.

GET (via the URL) and POST (usually a form submission)

You can alway get via $_GET http://php.net/manual/en/reserved.variables.get.php

or

$_POST http://nl.php.net/manual/en/reserved.variables.post.php

Comments

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.