[英]Insert into a database a single string value of a randomized array
I have this code:我有这段代码:
$possible_pics = array(
'red-pfp' => 'teem-pfp-red.svg',
'pink-pfp' => 'teem-pfp-pink.svg',
'blue-pfp' => 'teem-pfp-blue.svg',
'green-pfp' => 'teem-pfp-green.svg',
'purple-pfp' => 'teem-pfp-purple.svg',
'yellow-pfp' => 'teem-pfp-yellow.svg',
'orange-pfp' => 'teem-pfp-orange.svg',
);
shuffle($possible_pics);
echo reset($possible_pics);
The result of it, I want to insert it into a database this way:它的结果,我想以这种方式将它插入到数据库中:
$sentence = $connection->prepare("
INSERT INTO users (id, user, pass, email, profile_pic) VALUES (:id, :user, :password, :email, :profile_pic)
");
$sentence->execute(array(
':id' => $user_id,
':user' => $user,
':password' => $password,
':email' => $email,
':profile_pic' => $possible_pics
));
At this point I have already connected to the database, I'm just doing the SQL code.此时我已经连接到数据库了,我只是在做 SQL 代码。 As you can see, I am inserting the values into the database through an array, and in the part of :profile_pic
, I am saying that I want to insert the result of the first code I added to my question, where I am shuffling the array and it only brings us 1 value.如您所见,我通过数组将值插入数据库,在:profile_pic
部分,我说我想插入我添加到我的问题中的第一个代码的结果,我正在洗牌数组,它只给我们带来 1 个值。 The problem here is that when I run this, it shows this:这里的问题是,当我运行它时,它显示:
Notice: Array to string conversion on line 73
Why is this happening and how can I make so it inserts the value of the randomized array?为什么会发生这种情况,我怎样才能让它插入随机数组的值? Where I do that, it works perfectly, and it returns effectively only 1 value.在我这样做的地方,它完美地工作,并且它只有效地返回 1 个值。 I can't do an implode()
because it marks, unexpected implode()
.我不能做implode()
因为它标记了unexpected implode()
。
In resume, how can I insert into a database the returned value of the randomized array I showed at the beginning of my question?在简历中,如何将问题开头显示的随机数组的返回值插入数据库?
Just pick the output of reset
function to a variable and use it in your insert
statement.只需将reset
function 的 output 选择为一个变量,并在您的insert
语句中使用它。
shuffle($possible_pics);
$shuffledPic = reset($possible_pics);
...
$sentence->execute(array(
':id' => $user_id,
':user' => $user,
':password' => $password,
':email' => $email,
':profile_pic' => $shuffledPic
));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.