dongsuishou8039 2017-08-19 23:08
浏览 44

为什么我的准备声明不起作用?

For some reason, my prepare statement doesn't seem to return any rows or login, although I'm sure the password and username being entered is correct; when running the code without the prepare statement, the system logs in. Would anyone happen to know why my prepare statement isn't working, or if there is some other method I should use?

<?php session_start(); ?>
<!DOCTYPE html>
<html>
<head>
    <title>Logged in</title>
</head>
<body>
    <?php

        $user=$_GET["username"];
        $pass=$_GET["password"];


        $servername="localhost";
        $username="root";
        $password="";
        $dbName="db_artzytest";

        $conn=new mysqli($servername, $username, $password, $dbName);

        if($conn->connect_error){
            echo $conn->connect_error;
            die("connection to server not found");
        }else{
            echo "connection established";
        }
        ///finds account with matching login info
        $sql="SELECT * FROM db_users WHERE username='{$user}' AND password='{$pass}' ";


        $sql="SELECT * FROM db_users WHERE username = ? AND password = ?";


        try{
            $stmt = $conn->prepare($sql);
            $stmt->bind_param("ss", $user, $pass);
            $stmt->execute();
      }catch(Exception $e){
          die("prepare failed: " . $e);
      }
        echo 'here';
        //$result = $stmt->store_result();

        printf("Number of rows: %d. 
", $stmt->num_rows);

        //$result=$conn->query($sql);   
        echo 'here';
        if( mysqli_num_rows($result) > 0 ){

            while($row=mysqli_fetch_assoc($result)){
                //only logs in if the account is activated
                if($row["isActivated"]==1){

                    $_SESSION["currentUser"]=$user;
                    $_SESSION["currentId"]=$row["id"];
                    $_SESSION["currentPass"]=$pass;

                    //header( 'Location: ../ProfilePage/profilePage.php' );
                    header( 'Location: ../Content/displayGroup.php?group=general' );
                }else{
                    $_SESSION["m_Login"]="Unverified Account. Check your email to verify.";
                    header('Location: Login.php');
                }
            }
            /*
            $sql=" SELECT * FROM table_images WHERE userid = {$_SESSION["currentId"]} ";

            $result=$conn->query($sql);

            if(mysqli_num_rows($result) > 0){
                while($row=mysqli_fetch_assoc($result)){
                    echo "<img style='height: 10vh; width: 10vw;' src='../../images/{$row["id"]}.jpg' />";
                    echo "{$row["imageName"]}</br>";
                }
            }
            */

        }else{
            $_SESSION["m_Login"]="password or username incorrect {$user} {$pass}"  . var_dump(mysqli_stmt_get_result($stmt) );
            header('Location: Login.php');
            echo "no user found";
        }

    ?>
</body>
</html>
  • 写回答

1条回答 默认 最新

  • doulupian8725 2017-08-19 23:20
    关注

    You are mixing OO and procdeural so I'm going to assume that is what is causing your code not to execute as intended.

    The documentation states:

    Procedural style only: A result set identifier returned by mysqli_query(), mysqli_store_result() or mysqli_use_result().

    Change,

    if( mysqli_num_rows($result) > 0 ){
    

    To,

    if($stmt->num_rows($result) > 0 ) {
    

    That should fix the issue.

    评论

报告相同问题?

悬赏问题

  • ¥15 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀