Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/241.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从mysqli_fetch_数组向字符串提供值的PHP对象 公共静态函数实例化($record){ $object=新的自我; //$object->id=$record['UserID']; //$object->username=$record['username']; //$object->password=$record['password']; //$object->first_name=$record['Fname']; //$object->last_name=$record['Lname']; //$object->email=$record['email']; foreach(数组_唯一($record)作为$attribute=>$value){ 如果($object->has_属性($attribute)){ $object->$attribute=$value; //变量转储($attribute);回显“”; } } 返回$object; }_Php - Fatal编程技术网

从mysqli_fetch_数组向字符串提供值的PHP对象 公共静态函数实例化($record){ $object=新的自我; //$object->id=$record['UserID']; //$object->username=$record['username']; //$object->password=$record['password']; //$object->first_name=$record['Fname']; //$object->last_name=$record['Lname']; //$object->email=$record['email']; foreach(数组_唯一($record)作为$attribute=>$value){ 如果($object->has_属性($attribute)){ $object->$attribute=$value; //变量转储($attribute);回显“”; } } 返回$object; }

从mysqli_fetch_数组向字符串提供值的PHP对象 公共静态函数实例化($record){ $object=新的自我; //$object->id=$record['UserID']; //$object->username=$record['username']; //$object->password=$record['password']; //$object->first_name=$record['Fname']; //$object->last_name=$record['Lname']; //$object->email=$record['email']; foreach(数组_唯一($record)作为$attribute=>$value){ 如果($object->has_属性($attribute)){ $object->$attribute=$value; //变量转储($attribute);回显“”; } } 返回$object; },php,Php,在这个函数中,我通过两种方式从$object获取值。从注释部分开始的长距离和从未注释部分开始的短距离(obv) 长途跋涉会产生如下良好结果: 对象(用户)5(6){[“id”]=>string(1)“1”[“用户名”]=>string(6)“Parley”[“密码”]=>string(5)“12345”[“名字”]=>string(8)“克里斯蒂安”[“姓氏”]=>string(8)“Pirloaga”[“电子邮件”]=>string(27)“克里斯蒂安”。pirloaga@gmail.com“

在这个函数中,我通过两种方式从$object获取值。从注释部分开始的长距离和从未注释部分开始的短距离(obv)

长途跋涉会产生如下良好结果:

对象(用户)5(6){[“id”]=>string(1)“1”[“用户名”]=>string(6)“Parley”[“密码”]=>string(5)“12345”[“名字”]=>string(8)“克里斯蒂安”[“姓氏”]=>string(8)“Pirloaga”[“电子邮件”]=>string(27)“克里斯蒂安”。pirloaga@gmail.com“}

短途旅行的结果很糟糕,原因如下:

int(0) int(1) int(2) int(3) int(4) 内部(5) 国际(6) 对象(用户)#5(13){“id”=>NULL[“用户名”]=>NULL[“密码”]=>NULL[“名字”]=>NULL[“姓氏”]=>NULL[“电子邮件”]=>NULL[“0”]=>string(1)“1”[“1”]=>string(8)“克里斯蒂安”[“2”=>string(8)“皮尔洛加”[“3”=>string(27)”克里斯蒂安。pirloaga@gmail.com“[“4”]=>string(6)“Parley”[“5”]=>string(5)“12345”[“6”]=>string(19)“2016-02-1205:04:22”

int(1)…int(6)是$attribute值,后面是对象

我正在尝试将$attribute值与长期以来的那些字段进行匹配。我该怎么做? 这实际上是一段教程中的代码,对他有效,对我无效


我还有第二个问题。foreach循环总是将值加倍。在这种情况下,如果我删除array\u unique函数,它将在数据库中显示该结果两倍于它仅有的一行。这是为什么?

您可以通过使用
mysqli\u fetch\u object
而不是
mysqli\u fetch\u array
获得想要的结果

但要回答您的问题:如果您使用
mysqli\u fetch\u array
,默认情况下,您会得到一个包含数字键和关联键的数组。例如:

public static function instantiate($record){
     $object = new self;


    // $object->id = $record['UserID'];
    // $object->username = $record['Username'];
    // $object->password = $record['Password'];
    // $object->first_name = $record['Fname'];
    // $object->last_name = $record['Lname'];
    // $object->email = $record['Email'];

    foreach (array_unique($record) as $attribute => $value) {

        if($object->has_attribute($attribute)){

            $object->$attribute = $value;
            //var_dump($attribute);echo '<br>';
        }
    }
    return $object;
}
为了避免这种情况,您可以将第三个参数
int$result\u type
设置为
MYSQL\u ASSOC
,如下所述:

使用
MYSQL\u ASSOC
您将获得一个没有数字键的数组,例如:

array(
 1 => 'john',
 2 => 'doe',
 'firstname' => 'john',
 'lastname' => 'doe'
)

即使foreach中没有
array\u unique
,您的函数也可以运行。

我知道这不是您的问题,但是如果您想要包含数据的对象,请使用
mysqli\u fetch\u object
而不是
mysqli\u fetch\u array
。宽度mysqli\u ASSOC我得到了这个坏结果:string(6)“UserID”string(5)“Fname”string(5) “Lname”字符串(5)“Email”字符串(8)“Username”字符串(8)“Password”字符串(9)“Timestamp”对象(用户)#5(13){[“id”]=>NULL[“Username”]=>NULL[“first_name”]=>NULL[“last_name”]=>NULL[“Email”]=>NULL[“UserID”=>string(1)“1”[“Fname”]=>string(8)“Cristian”[“Lname”=>string(8)“Email”=>27”)”“克里斯蒂安。pirloaga@gmail.com“[“用户名”]=>string(6)“密码”]=>string(5)“12345”[“时间戳”]=>string(19)“2016-02-12 05:04:22”}非常感谢Mario A为mysql_对象提供的替代方案。现在我正在关注本教程,了解它的工作原理,但最后我将进行此升级。
array(
 'firstname' => 'john',
 'lastname' => 'doe'
)