SQL编写的语句。 PHP

时间:2017-08-05 08:04:27

标签: php sql

可能这个问题会是一些“愚蠢的问题”,但仍然...... 我是PHP和SQL的新手,我无法理解我在这里做错了什么:

if(isset($_POST[$logButton])) //Checking for login button pressed
    {
        //Retrieving information from POST method
        $uid = $_POST['login'];
        $upwd = $_POST['password'];
        //SQL Connection
        $mysqli = new mysqli('localhost', 'root', '', 'students');
        if(!$mysqli)
        {
            echo "<h1 class='h1A'>Problem accured while connecting to the DB. " . mysqli_error($mysqli) . "</h1>"; //!!!Delete displaying error msg after dev.
        }else
        {
            $sql = "SELECT * FROM login_data WHERE login = ? AND password = ?"; //SQL query
            $stmt = $mysqli->prepare($sql) or die("error1"); //No error
            $stmt->bind_param('ss', $uid, $upwd) or die("error2");//No error

            $stmt->execute() or die("error3");//Giving DB query. No error
            $result = $stmt->fetch() or die("error4".mysqli_error($mysqli)); //Putting query's result into assoc array. !!!Delete displaying error msg after dev. No error
            echo print_r($result); //It prints out "11" ? ? ?
            if(count($result['id']) < 1) //If no rows found.
            {
                echo "<h1 class='h1A'>Couldn't find account. Please, recheck login and password.</h1>";
                die();
            }elseif($result['id'] > 1)//If more then 1 row found.
            {
                echo "<h1 class='h1A'>Caught 9090 error. Contact the administrator, please.".mysqli_error($mysqli)."</h1>";
                die();
            }elseif($result['id'] == 1) //If only one row's been found.
            {
                $_SESSION['isLoggedIn'] = true;
                redirectTo('/index.php'); //Declared function.
                die();
            }
        }
    }

这是lib.php文件中处理函数的一部分。该文件包含在html页面中,并使用该函数。没有显示错误,当我print_r $ result时 - 它打印出来11.无法得到它。

1 个答案:

答案 0 :(得分:2)

好吧,使用 print_r 而不使用 echo

print_r($result);

或将第二个参数传递给 print_r 函数,以便它可以返回字符串:

echo print_r($result, true);

有关详细信息,请参阅http://php.net/manual/en/function.print-r.php