为什么我的代码不是从我的数据库发布数据?

时间:2017-06-16 16:33:24

标签: php mysql mysqli

因此,在我的login页面成员中,我尝试根据登录用户动态从SQL数据库中提取数据。我通过使用会话变量确定哪个用户登录。但是,以下从数据库中检索名称的代码是空白的,有人可以解释原因吗?

<tr>
                <td>Name</td>
              <td>
              <?php
                $offset = $argv[0];
                $connect = mysqli_connect("****.****", "*****", "******", "****");
                $output = '';
                if(isset($_SESSION["username"])){
                  $username = $_SESSION["username"];
                }
                else {
                  $username = $_SESSION["regusername"];
                }
                $query = "SELECT name FROM users WHERE username = $username $offset";
                $output = mysqli_query($connect, $query);
                echo $output;
              ?>
           </td>
              </tr>

1 个答案:

答案 0 :(得分:1)

首先使用预先准备好的声明,如下所示:

<?php
    $conn = mysqli_connect("****.****", "*****", "******", "****");

  // Check connection
  if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

    if(isset($_SESSION["username"])){
      $username = $_SESSION["username"];
    }
    else {
      $username = $_SESSION["regusername"];
    }

    $stmt = $conn->prepare("SELECT name FROM `users` WHERE username=?");
    $stmt->bind_param("s",$username);
    $stmt->execute();
    $stmt->bind_result($output);
    while($stmt->fetch()) {
        echo $output;
    }//end while
    $stmt->close();
?>