简体   繁体   English

无法从MySQL数据库检索值

[英]Having trouble retrieving values from MySQL database

I'm very new to using MySql and am having trouble retrieving values from my database. 我对使用MySql非常陌生,无法从数据库中检索值。 I was under the impression that i was going about it the correct way but my echo statements don't print anything. 我的印象是我正在以正确的方式进行操作,但是我的echo语句不打印任何内容。

I'd appreciate some help. 我将不胜感激。 My code is below. 我的代码如下。 I know i'll have to add security later on like sanitizing user input. 我知道以后必须像清理用户输入一样增加安全性。

<?php
            $email = $_POST['email'];
            $password = $_POST['password'];
            $hashedPass = sha1($password);

            if ((!isset($email)) || (!isset($password))) {
                //Visitor needs to enter a name and password
                echo "Data not provided";
            } else {
                echo "Received details $email and $password <br/>";
                // connect to mysql
                $mysql = mysqli_connect("localhost", "root", "root");
                if(!$mysql) {
                echo "Cannot connect to PHPMyAdmin.";
                exit;
                } else {
                echo "Connected to phpmyadmin <br/>";
                }
            }
            // select the appropriate database
            $selected = mysqli_select_db($mysql, "languageapp");
            if(!$selected) {
                echo "Cannot select database.";
                exit;
            } else {
                echo "DB Selected"; 
            }
            // query the database to see if there is a record which matches
            $query = "select count(*) from user where email = '".$email."' and password = '".$hashedPass."'";
            $result = mysqli_query($mysql, $query);
            if(!$result) {
                echo "Cannot run query.";
                exit;
            }

            $row = mysqli_fetch_row($result);
            $count = $row[0];

            $userdata = mysqli_fetch_array($result, MYSQLI_BOTH);
            echo $userdata[3];
            echo $userdata['firstName'];

            if ($count > 0) {   
                echo "<h1>Login successful!</h1>";
                echo "<p>Welcome.</p>";
                echo "<p>This page is only visible when the correct details are provided.</p>";
            } else {
                // visitor's name and password combination are not correct
                echo "<h1>Login unsuccessful!</h1>";
                echo "<p>You are not authorized to access this system.</p>";
            }
            ?>

I believe the problem is that you call twice the * fetch * family function which will cause the $userdata to be empty. 我相信问题是您调用了两次* fetch *系列函数,这将导致$ userdata为空。

From the documentation mysql_fetch_row will fetch the next row and move the internal data pointer ahead. 从文档mysql_fetch_row将获取下一行并将内部数据指针向前移动。 So when you call mysqli_fetch_array($result, MYSQLI_BOTH) and I suppose the user/password is unique there is nothing to retrieve. 因此,当您调用mysqli_fetch_array($ result,MYSQLI_BOTH)时,我想用户名/密码是唯一的,没有任何可检索的内容。 Also another mistake you did is that your SELECT doesn't retrieve the actual user data, but just the count number for the user/password combination. 您犯的另一个错误是,SELECT不会检索实际的用户数据,而只会检索用户/密码组合的计数。 So your userdata will be always incorrect, even if you fetch the data right. 因此,即使您正确获取数据,您的用户数据也总是不正确的。

So change your query to something like that: 因此,将查询更改为类似的内容:

 $query = "select * from user where email = '".$email."' and password = '".$hashedPass."' LIMIT 1";

Then use mysql_fetch_array to check if the entry exist and then retrieve the user data. 然后使用mysql_fetch_array检查条目是否存在,然后检索用户数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM