簡體   English   中英

mysqli查詢將列名稱返回為php中的一行

[英]mysqli query returns the column name as one row in php

這是我的login.php文件

<?php require ("database_connect.php");?>
<!DOCTYPE html>
<html>
<body>  
    <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"])?>">
    Name : <input type="text" name="name"><br/>
    Password : <input type = "text" name="password"><br/>
    <input type="submit" name="login" value="Log In">   
    </form>

    <?php
    $name=$password="" ;
        if($_SERVER["REQUEST_METHOD"]=="POST" and isset($_POST["login"])){
            $name = testInput($_POST["name"]);
            $password = testInput($_POST["password"]);
        }//if ends here

        //testInput function
        function testInput($data){
            $data = trim($data);
            $data = stripslashes($data);
            $data = htmlspecialchars($data);
        }//testInput ends here


        if(isset($_POST["login"]) && isset($_POST["name"]) && isset($_POST["password"]) && !empty($_POST["name"]) && !empty($_POST["password"])){

        //echo "Name ".$_POST["name"];

        if($result = mysqli_query($conn,"SELECT * FROM users WHERE name='$name' and password='$password'")){            
            if($result->num_rows > 1){
            echo "you are logged in";
                while ($row = $result->fetch_assoc()){
                    echo "Name ".$row["name"]."-Password ".$row["password"];
                }//while loop ends here  
            }//if ends here


            /* free result set */
            $result->close();
        }       
        else{
            print "Wrong Credentials "."<br>";
            die(mysqli_error($conn));
        }
        }       
        //close connection
        $conn->close();
    ?>
</body>
</html>

一個問題是我的查詢
if($result = mysqli_query($conn,"SELECT * FROM users WHERE name='$name' and password='$password'"))將列名返回為一行。 不知道還可以嗎?
不管是我輸入了錯誤的名稱或密碼還是正確的,在這兩種情況下,我都沒有得到任何輸出。 我在這里做錯了什么?

並且,如果可以的話,請告訴我如何使用正確的示例以正確的格式在phpi中編寫mysqli查詢。 我在google上搜索,但是有不同的方法,所以當查詢中使用列名和變量時,我尤其感到困惑。

您的test_input函數是弱函數/不安全的,此外,還描述了mysql_query,請使用mysqli和准備好的語句,如此處所述: http : //php.net/manual/zh/mysqli.prepare.php

此外,我還提供了一部分用於登錄系統的代碼(使用鹽等會更加復雜,您應該能夠在一段適合您的腳本中對其進行編譯。

//get salt for username (also check if username exists)
        $stmtfc =  $mysqli->stmt_init();
        $prep_login_quer = "SELECT salt,hash,lastlogin FROM users WHERE name=? LIMIT 1";
        $stmtfc->prepare($prep_login_quer);
        $stmtfc->bind_param("s", $username);
        $stmtfc->execute() or die("prep_login_quer error: ".$mysqli->error);
        $stmtfc->store_result();
        if ($stmtfc->num_rows() == 1) {
            $stmtfc->bind_result($salt,$hash,$lastlogin);
            $stmtfc->fetch(); //get salt
            $stmtfc->free_result();
            $stmtfc->close();

嘗試更改此查詢

$result = mysqli_query($conn,"SELECT * FROM users WHERE name='$name' and password='$password'")

to

$result = mysqli_query($conn,"SELECT * FROM users WHERE name='$name' and password='$password' limit 1")

那么您只會得到一行,並嘗試更改

$row = $result->fetch_assoc()

to

$row = $result->mysqli_fetch_row()

那么您可以按列號而不是列名顯示結果

<?php 
  mysql_connect("abc.com","user","password");
  mysql_select_db("database name"); 
  $query1="select * from table_name"; 
  $exe1= mysql_query($query1); 
  $row= mysql_fetch_assoc($exe1); 
  if($row["email"]==$_POST["email"] && $row["[password"]==$_POST["password"]) { 
    echo "Login successfully"; 
  } else {
    echo "error in login"; 
  } 
?>

row["email"]$row["password"]輸入您的列名

我不知道你是什么意思,但這就是我查詢mysqli的方式

$query = mysqli_query($db, "SELECT * FROM users WHERE name='$name' AND password='$password'");

if($query && mysqli_affected_rows($db) >= 1) { //If query was successfull and it has 1 or more than 1 result 
echo 'Query Success!';
//and this is how i fetch rows
while($rows = mysqli_fetch_assoc($query)) {
   echo $rows['name'] . '<br />' ;
}

} else {
echo 'Query Failed!';
}

我認為那是你的意思

編輯:

<?php require ("database_connect.php");?>
<!DOCTYPE html>
<html>
<body>  
    <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"])?>">
    Name : <input type="text" name="name"><br/>
    Password : <input type = "text" name="password"><br/>
    <input type="submit" name="login" value="Log In">   
    </form>

    <?php
    $name = null ;
    $password= null ;
        if($_SERVER["REQUEST_METHOD"]=="POST" and isset($_POST["login"])){
            $name = mysqli_real_escape_string($conn, $_POST["name"]); //I updated that because your variables are not safe
            $password = mysqli_real_escape_string($conn, $_POST["password"]);
        }//if ends here

        //testInput function
        function testInput($data){
            $data = trim($data);
            $data = stripslashes($data);
            $data = htmlspecialchars($data);
        }//testInput ends here


        if(isset($_POST["login"]) && isset($_POST["name"]) && isset($_POST["password"]) && !empty($_POST["name"]) && !empty($_POST["password"])){

        if($result = mysqli_query($conn,"SELECT * FROM users WHERE name='{$name}' and password='{$password}'")){            

            print "rows are ".mysqli_num_rows($result)"<br>";//number of rows

            if($result && mysqli_affected_rows($conn) >= 1){//If query was successfull and it has 1 or more than 1 result
            echo "you are logged in<br>";
                while ($row = mysqli_fetch_assoc($result)){
                    echo "Name ".$row["name"]."-Password ".$row["password"];
                }//while loop ends here  
            }//if ends here


            /* free result set */
            mysqli_free_result($result);
        }       
        else{
            print "Wrong Credentials "."<br>";
            die(mysqli_error($conn));
        }
        }       
        //close connection
        mysqli_close($conn);
    ?>
</body>
</html>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM