Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/226.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 无法显示用户名_Php_Html_Mysqli - Fatal编程技术网

Php 无法显示用户名

Php 无法显示用户名,php,html,mysqli,Php,Html,Mysqli,我在试图从$\u GET在配置文件页上显示用户名时遇到问题,会话已创建,但无法在配置文件页上显示用户名 <?php include("template/header.php"); include("requires/connection.php"); ?> <?php if(isset($_GET['u'])) { $username = mysqli_real_escape_string($_GET['u']); if

我在试图从$\u GET在配置文件页上显示用户名时遇到问题,会话已创建,但无法在配置文件页上显示用户名

    <?php include("template/header.php");
include("requires/connection.php"); ?>

    <?php
    if(isset($_GET['u'])) {
        $username = mysqli_real_escape_string($_GET['u']);
        if(ctype_alnum($username)) {
         // Check user exists
          $check = mysqli_query("SELECT username FROM users WHERE username='$username'");
    if(mysqli_num_rows($check)===1) {
            $get = mysqli_fetch_assoc($check);
            $username = $get['username'];
          }
          else {
            echo "<h2>User doesn't exist</h2>";
            exit();
          }
        }
      }
    ?>

      <div class="profile-content-container">
         <h1>Profile <?php echo "$username"; ?></h1>
         <p>Welcome</p>    
      </div><!-- end of profile-content-container -->

      <div class="profileMenu">
            <div id="leftsideMenu">
              <ul>
                <li><a href="home.php">Home</a></li>
                <li><a href="profile.php">My Profile</a></li>
                <li><a href="#"></a></li>
                <li><a href="#"></a></li>
                <li><a href="logout.php">Logout</a></li>
              </ul><!-- end of menu -->
            </div><!-- end of leftsideMenu -->
          </div><!-- end of profileMenu -->    
    <?php include("template/footer.php"); ?>


mysqli\u real\u escape\u string()
mysqli\u query()
函数都要求将DB连接作为第一个参数传递;你的没有

mysqli_real_escape_string($con, $_GET['u'])
                          ^^^^^ see that?
所以

和查询

mysqli_query($con, "SELECT ...
             ^^^^^ see that?
我在这里使用了
$con
,因为没有指示您使用哪个变量来分配连接

参考资料:

旁注:您可能需要更改此选项

if(mysqli_num_rows($check)===1)

在某些情况下,我看到
==1
失败

另一件事,改变这段代码

$get = mysqli_fetch_assoc($check);
$username = $get['username'];
到,并使用
while
循环

while($get = mysqli_fetch_assoc($check)){
         // or mysqli_fetch_array

    $username = $get['username'];

}
另外,您的数据库连接应该基于
mysqli\uuu
,而不是
mysql\uu
或PDO,这对我们来说也是未知的

  • 这些不同的MySQL API不会混合使用
参考资料:


或die(mysqli_error($con))
添加到
mysqli_query()
以检查错误

添加到文件的顶部,这将有助于查找错误

<?php 
error_reporting(E_ALL);
ini_set('display_errors', 1);

// rest of your code

因此,对其使用条件语句:

<h1>Profile 
<?php
if(isset($username) && !empty($username)) {

    echo $username;
}

else{
    echo "Username is empty or not set somewhere.";
}

?>
</h1>
Profile
或者使用

Profile

Profile

Profile

Profile

如您在评论中所述,您使用的是
?u=“username”
,该词应不带引号阅读
?u=username
,例如
?u=john
不带引号,可能区分大小写。因此
john
可能与大写字母为“J”的
john
不同。

提示:将db连接传递给函数。这不是
MySQL \
-“我们不再是堪萨斯州了”,谁知道你首先连接的是哪个MySQL API,以及你的GET数组是否失败。使用最新版本的MySQL API。你只是做了所谓的“隐形”编辑,并在编辑中使用我的建议,而没有将其标记为编辑。反过来,我也反对被否决,人们会说“他在连接,为什么答案是这样?”。我执行了回滚到上一个修订版的操作。请不要这样做。我在这两行中添加了连接变量,但仍然没有显示用户名。我使用$con进行连接。@Andy自从您第一次看到它以来,我已经对它进行了一些编辑,并且考虑到表单的可能使用。如果是这样的话,你需要发布它。如果您使用任何其他方法使其通过GET数组,我需要查看它是如何使用的。通常,它的作用是
file.php?u
等。使用错误报告,如我的回答中所述,并检查查询中的错误。@Andy如果您使用的是JS/Ajax,但没有发布,则需要显示
$con
这么说并不能告诉我太多,看不到你是如何连接的。@Andy你还没有使用
$username=mysqli\u real\u escape\u string($\u GET['u')-如果是,你没有读我的答案。我需要知道GET是从哪里来的。否则,我将无法帮助您,因为我已经知道了您所发布的内容。@Andy和
?u=“username”
应该不加引号地阅读
?u=username
,或者作为一个例子
?u=john
没有引号,并且可能区分大小写。因此,
john
可能与大写为“J”的
john
不同。
<?php 
error_reporting(E_ALL);
ini_set('display_errors', 1);

// rest of your code
<input type="text" name="u">
<h1>Profile <?php echo "$username"; ?></h1>
<h1>Profile 
<?php
if(isset($username) && !empty($username)) {

    echo $username;
}

else{
    echo "Username is empty or not set somewhere.";
}

?>
</h1>
<h1>Profile <?php echo $username ? $username : ""; ?></h1>
<h1>Profile <?php echo $username ? $username : "It is empty"; ?></h1>
<h1>Profile <?php echo !empty($_GET['u']) ? $_GET['u'] : ""; ?></h1>
<h1>Profile <?php echo !empty($_GET['u']) ? $_GET['u'] : "It is empty."; ?></h1>