用户名不是来自Dataabase

时间:2017-07-08 10:56:18

标签: php mysql

我是php的新手。我有些问题。我用($ _GET)变量获取用户名的值。让我们假设某人在网址中输入(mehwish)。我想检查用户输入的用户名是否在数据库中可用。我的代码不起作用。纠正我的错误,如果有的话。感谢您的支持。

{{1}}

4 个答案:

答案 0 :(得分:0)

使用类似的东西:

<?php 
session_start(); //This always need to be first line of code
include("header.php");
include("classes/db.php");

$username="";
if(isset($_GET['username']))
{
    $user = mysqli_real_escape_string($con,$_GET['username']);
    //get data from table using the passed user name in the url
    $checkUser = mysqli_query($con,"SELECT user_name FROM user_reg WHERE user_name='$user'") or die(mysqli_error($con));
    //return number of rows from the above query
    //if more than 0 means user exist in database table
    if(mysqli_num_rows($checkUser)>0)
    {
        $row = mysqli_fetch_array($checkUser);
        $username = $row['user_name'];
        //use like this to store in session using $_SESSION global variable
        $_SESSION["firstname"] = $row["firstname"]; //firstname - column name in table
        $_SESSION["lastname"] = $row["lastname"];
        $_SESSION["email"] = $row["email"];
    }
    else
    {
        echo "User does not exist";
    }
}
?>

答案 1 :(得分:0)

尝试做这样的事情 -

<?php
//checking for GET variable
if(isset($_GET['username'])) $user=$_GET['username'];
else exit("Username is missing");

//IGNORE this if your DB connection is set from other files
//If so make a connection call and get the connection object.
$servername = "localhost";  //your db servername
$username = "username";   //your db username
$password = "password";   //your db password
$dbname = "myDB";   //your db name

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

//Your Query
$sql = "SELECT user_name FROM user_reg WHERE user_name='$user'";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    //  found a row with the user entry.
    //  then the username exists in the database.
    echo "Valid User";

} else {
    echo "User not found";
}
$conn->close();
?>

答案 2 :(得分:0)

<?php 
$username="";
if(isset($_GET['username']))
{
$user=$_GET['username'];
$checkUser = mysqli_query($con,"SELECT user_name FROM user_reg WHERE 
user_name='$user'") or die(mysqli_error($con));
$row=mysqli_fetch_assoc($checkUser);
if(mysqli_num_rows($checkUser)>0){
    $username = $row["user_name"];
}else{
    echo "User does not exist";
}
}
?>

答案 3 :(得分:-1)

你的代码中有很多错误。尝试这样的事情:

template