php 如何从 URL 中获取价值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18944318/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 18:32:07  来源:igfitidea点击:

How get value from URL

phpmysqldatabaseurl

提问by Vaja Axobadze

I want to get value from URL to choose data from database by ID. I want value for id.

我想从 URL 获取值以通过 ID 从数据库中选择数据。我想要 id 的价值。

For example if I open www.example.com/index.php?id=12
I want to get value whose id = 12 in database.

例如,如果我打开www.example.com/index.php?id=12
我想获取数据库中 id = 12 的值。

If I open www.example.com/index.php?id=7
get value whose id = 7 in database and so on.

如果我www.example.com/index.php?id=7
在数据库中打开id = 7 的值,依此类推。

I have some code :

我有一些代码:

$results = mysql_query("SELECT * FROM next WHERE id=$id");    
while ($row = mysql_fetch_array($results))     
{       
    $url = $row['url'];
    echo $url;    
}

回答by Steven

Website URL:

网址:

http://www.example.com/?id=2

Code:

代码:

$id = intval($_GET['id']);
$results = mysql_query("SELECT * FROM next WHERE id=$id");    
while ($row = mysql_fetch_array($results))     
{       
    $url = $row['url'];
    echo $url; //Outputs: 2
}

回答by zur4ik

There are two ways to get variable from URL in PHP:

在 PHP 中有两种从 URL 获取变量的方法:

When your url is: http://www.example.com/index.php?id=7you can get this idvia $_GET['id']or $_REQUEST['id']command and store in $idvariable.

当您的 url 是:http://www.example.com/index.php?id=7您可以id通过$_GET['id']$_REQUEST['id']命令获取并存储在$id变量中。

Lest's take a look:

让我们来看看:

// url is www.example.com?id=7

//get id from url via $_GET['id'] command:
$id = $_GET['id']

same will be:

同样将是:

//get id from url via $_REQUEST['id'] command:
$id = $_REQUEST['id']

difference is that variable can be passed to file via url or via POSTmethod.

不同之处在于变量可以通过 url 或通过POST方法传递给文件。

if variable is passed through url, then you can get it with $_GET['variable_name']or $_REQUEST['variable_name']but if variable is posted, then you need to you $_POST['variable_name']or $_REQUST['variable_name']

如果变量是通过 url 传递的,那么您可以使用$_GET['variable_name']or获取它,$_REQUEST['variable_name']但是如果发布了变量,那么您需要使用$_POST['variable_name']$_REQUST['variable_name']

So as you see $_REQUEST['variable_name']can be used in both ways.

因此,如您所见,$_REQUEST['variable_name']可以以两种方式使用。

P.S:Also remember- never do like this: $results = mysql_query("SELECT * FROM next WHERE id=$id");it may cause MySQL Injection and your database can be hacked.

PS:还要记住- 永远不要这样做:$results = mysql_query("SELECT * FROM next WHERE id=$id");它可能会导致 MySQL 注入,并且您的数据库可能会被黑客入侵。

Try to use:

尝试使用:

$results = mysql_query("SELECT * FROM next WHERE id='".mysql_real_escape_string($id)."'");

回答by SharpKnight

You can get that value by using the $_GETarray. So the id value would be stored in $_GET['id'].

您可以使用$_GET数组获取该值。因此 id 值将存储在$_GET['id'].

So in your case you could store that value in the $idvariable as follows:

因此,在您的情况下,您可以将该值存储在$id变量中,如下所示:

$id = $_GET['id'];

回答by djot

You can access those values with the global $_GET variable

您可以使用全局 $_GET 变量访问这些值

//www.example.com/index.php?id=7
print $_GET['id']; // prints "7"

You should check all "incoming" user data - so here, that "id" is an INT. Don't use it directly in your SQL (vulnerable to SQL injections).

您应该检查所有“传入”用户数据 - 所以在这里,“id”是一个 INT。不要直接在您的 SQL 中使用它(容易受到 SQL 注入的影响)。

回答by Dinesh

You can also get a query string value as:

您还可以获取查询字符串值:

$uri =  $_SERVER["REQUEST_URI"]; //it will print full url
$uriArray = explode('/', $uri); //convert string into array with explode
$id = $uriArray[1]; //Print first array value