使用PHP OOP从数据库中检索mysql中的信息

时间:2022-09-25 23:16:37

I am very confused about this problem I am having. I am trying to get information from my database, but it only partly works. I have a function named authenticate in my user.php file in a class of User that extends DatabaseObject.

我对这个问题非常困惑。我试图从我的数据库中获取信息,但它只是部分工作。我在user.php文件中的一个名为authenticate的函数在一个扩展DatabaseObject的User类中。

class User extends DatabaseObject{

protected static $table_name = "sellatext_users";  
public $id;  
public $email;
public $password;
public $fname;
public $lname;

public static function authenticate($email="", $password="") {
   global $database;
   $email = $database->escape_value($email);
   $password = $database->escape_value($password);

   $sql  = "SELECT * FROM ".static::$table_name;
   $sql .= " WHERE email = '{$email}' ";
   $sql .= "AND password = '{$password}' ";
   $sql .= "LIMIT 1";

   $result_array = static::find_by_sql($sql);
   var_dump($result_array);
   die;
   return !empty($result_array) ? array_shift($result_array) : false;
}

This works fine, and if I var_dump($result_array) I get

这工作正常,如果我得到var_dump($ result_array)

array(1) { 
       [0]=> object(User)#5 (5) 
               { 
                 ["id"]=> string(1) "3" 
                 ["email"]=> string(18) "email_address" 
                 ["password"]=> string(8) "XXXXXXXX" 
                 ["fname"]=> NULL 
                 ["lname"]=> NULL 
               }
         }

Email_address and password are omitted for security, but I can't figure out why I am getting NULL for both the fname and the lname. There is information in there in the table (it is my name) since I can access the table through Navicat. Both fields are varchar fields.

出于安全考虑,省略了Email_address和密码,但我无法弄清楚为什么我为fname和lname都获取NULL。表中有信息(这是我的名字),因为我可以通过Navicat访问该表。这两个字段都是varchar字段。

Can anyone point me in the right direction please?
Thank you.

有人能指出我正确的方向吗?谢谢。

1 个解决方案

#1


0  

Change

$sql  = "SELECT * FROM ".static::$table_name;

To

$sql  = "SELECT * FROM ".self::$table_name;

Also,

$result_array = static::find_by_sql($sql);

to

$result_array = self::find_by_sql($sql);

You need to use word self instead of static

你需要使用word self而不是static

#1


0  

Change

$sql  = "SELECT * FROM ".static::$table_name;

To

$sql  = "SELECT * FROM ".self::$table_name;

Also,

$result_array = static::find_by_sql($sql);

to

$result_array = self::find_by_sql($sql);

You need to use word self instead of static

你需要使用word self而不是static