Mysql获取随机用户名 [英] Mysql get random username

查看:48
本文介绍了Mysql获取随机用户名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要获取一个随机用户名并转到 view.php?name=USERNAME 我哪里错了?

I need to get a random user name and go to view.php?name=USERNAME where am I wrong?

<form action="view.php?name=<?php $getname; ?>" method="get">
    <input class="button" type="submit" name="submit" value="RANDOM SKIN" />
</form>
<?php
    include('config.php');
    if ($_POST['submit']) {
        $sql1 = mysql_query("SELECT * FROM `skins` ORDER BY rand() limit 1") or die(mysql_error());
        while ($row2 = mysql_fetch_assoc($sql1)) {
            $getname = $row2['username'];
            echo $getname;
        }
    }
?>

推荐答案

除了 $_GET$_POST 之间交替的问题,如果你想保持你的GET 请求中的键值对,这不是什么大问题.您有多种选择:

Aside from the issue of alternating between $_GET and $_POST, if you want to keep your key value pairs from your GET request, this isn't a big issue. You have multiple options:

推荐

查看parse_url().这从 PHP 4 开始可用.文档中有很多示例:

Check out parse_url(). This is available from PHP 4 onward. The documentation has plenty of examples:

$url = 'http://username:password@hostname/path?arg=value#anchor';

print_r(parse_url($url));

echo parse_url($url, PHP_URL_PATH);

替代方案

在您的 $_SERVER 变量上尝试 var_dump().您应该能够看到可以使用 $_SERVER['QUERY_STRING'] 访问的 QUERY_STRING 属性.在这里,您可以使用手动拆分键/值.你猜怎么着,还有一个功能!

Try a var_dump() on your $_SERVER variables. You should be able to see a QUERY_STRING attribute which you can access with $_SERVER['QUERY_STRING']. Here, you can manually split your key / values using. Guess what, there's a function for that too!

查看:parse_str().你给它一个查询字符串,就像 GET 一样,它会为你整理出所有的键值.查看文档,它为您提供了以下示例:

Check out: parse_str(). You give it a query string, like that from a GET, and it'll sort out all your key values for you. Check out the documentation, it gives you the following example:

$str = "first=value&arr[]=foo+bar&arr[]=baz";
parse_str($str);
echo $first;  // value
echo $arr[0]; // foo bar
echo $arr[1]; // baz

真的很简单!尝试这两个功能 - 它们可以解决您的确切问题.为了让上述工作为您服务,我会先尝试:

Pretty simple really! Try both these functions - they're there to solve your exact problem. To get the above working for you, I would first try:

var_dump(parse_str($_SERVER['QUERY_STRING']));

最后,我将成为谈论 MySQL 弃用的人.这是我复制/粘贴给新手的内容:

In closing, I'll be the one to talk about MySQL deprecation. Here's what I copy / paste to the newbies:

请不要在新代码中使用mysql_*函数.它们不再维护,正式弃用,并且可以在实时代码中存在危险.看到红框?了解准备好的语句,并使用PDOMySQLi> - 这篇文章 将帮助您决定哪个.如果您选择 PDO,这里有一个很好的教程.

Please, don't use mysql_* functions in new code. They are no longer maintained, are officially deprecated, and can be dangerous in live code. See the red box? Learn about prepared statements instead, and use PDO, or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial.

这篇关于Mysql获取随机用户名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆