【问题标题】:How to put the values of a array pulled from mySQL into variables如何将从 mySQL 中提取的数组的值放入变量中
【发布时间】:2016-05-04 08:05:50
【问题描述】:

我已经能够从 MySQL 数据库中查询数据并将数组放入变量 $results,但是我无法将数组的一部分放入 $results 变量中,例如密码或注册时间成单独的变量。我做错了什么?

这是我的数据库类中的选择函数,我使用 $query 的参数从数据库中查询数据。这适用于我的帖子页面,所以我认为这不是问题。

// Select Function
        
        public function select($query){
            $result = $this->connection->query($query);
            
            while ( $obj = $result->fetch_object()){
                $results[] = $obj;
            }
            
            return $results;
        }

这是我的查询类中的登录函数,我将提交的用户名放入查询中,然后将其传递给上面的选择函数以使用该用户名提取结果,该用户名返回到登录函数并存储在$结果变量。然后它检查变量是否为空,如果是它会杀死脚本,这一切都在我得到的工作: 1)yay!!!! 2)print_r()显示的内容是:

Array ( [0] => stdClass Object ( [ID] => 1 [username] => dillon [fname] => Dillon [lname] => Erhardt [email] => dillon@erhardt.co.uk [password] => dilandsas [regtime] => 1453685794 ) ) 

function login($redirect) {
			global $db;
            
            if(!empty($_POST)) {
                
                $username = htmlspecialchars($_POST['username']);
                $password = htmlspecialchars($_POST['password']);
                
                $query = "
                      SELECT * FROM users
                      WHERE username = '$username'
                    ";
            
                $results = $db->select($query);


				//Kill the script if the submitted username doesn't exit
				if (empty($results)) {
					die('Sorry, that username does not exist!');
				} 
                
                echo "yay";
                print_r($results);


				//The registration date of the stored matching user
				$storeg = $results['regtime'];

				//The hashed password of the stored matching user
				$stopass = $results['password'];

问题是,当我尝试使用 $results['password'];$results['regtime']; 将密码或 regtime 中的值存储在我的 $storeg 和 $stoppass 变量中时,我会收到这些通知

Notice: Undefined index: regtime in /Applications/MAMP/htdocs/Test/CMS-v2/includes/class-query.php on line 55

Notice: Undefined index: password in /Applications/MAMP/htdocs/Test/CMS-v2/includes/class-query.php on line 58
Invalid Login

如何将数组中的密码和注册时间值存储到 $results 变量中?

【问题讨论】:

  • 请列出您的“用户”表字段吗?

标签: php html mysql arrays database


【解决方案1】:

请尝试以下代码。

function login($redirect) {
            global $db;

            if(!empty($_POST)) {

                $username = htmlspecialchars($_POST['username']);
                $password = htmlspecialchars($_POST['password']);

                $query = "
                      SELECT * FROM users
                      WHERE username = '$username'
                    ";

                $results = $db->select($query);


                //Kill the script if the submitted username doesn't exit
                if (empty($results)) {
                    die('Sorry, that username does not exist!');
                } 

                echo "yay";
                print_r($results);


                //The registration date of the stored matching user
                $storeg = isset($results[0]->regtime) ? $results[0]->regtime : "";

                //The hashed password of the stored matching user
                $stopass = isset($results[0]->password) ? $results[0]->password : "";

【讨论】:

    【解决方案2】:

    在您的选择中,您从结果集中获取一个对象并将其添加到一个数组 (result)。

    因此,您需要从结果数组中获取对象并引用其中的一个属性:

    $storeg = $results[0]->regtime;
    

    这显然只有在$result 包含数据库中的一行且恰好一行的情况下才有效。所有其他情况(无行、三行)都会由于缺少错误处理而导致错误。

    【讨论】:

    • 谢谢。这正是我所需要的。
    猜你喜欢
    • 2020-04-15
    • 2021-01-06
    • 2020-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多