首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将关联数组传递给php函数

问将关联数组传递给php函数
EN

Stack Overflow用户
提问于 2015-03-30 02:50:47
回答 2查看 690关注 0票数 2

如何将关联数组传递给php函数?我尝试了下面的代码,但它不起作用。请帮助我,或者至少给我建议。非常感谢。

代码语言:javascript
复制
addrecord.php
require 'class.php';
$fields = array(
'strname' =>$name,
'strdescript' =>$strdescript,
'straction' =>$straction
);
$addRecord = new Record();
$addRecord->addRecord($fields);


class.php
class Record{
public $db, $sql,$stmt;
public function __construct(){
$this->db = new connection();
$this->db = $this->db->dbConnect();
}
public function addRecord($fields){
$this->sql =
"INSERT INTO user
(name,description,action)
VALUES
(?,?,?)"
$this->stmt = $this->db->prepare($this->sql);
$this->stmt->bindParam(1,{$fields['strname']},PDO::PARAM_STR);
$this->stmt->bindParam(2,{$fields['strdescript']},PDO::PARAM_STR);
$this->stmt->bindParam(3,{$fields['straction']},PDO::PARAM_STR);
$this->stmt->execute();
if($this->stmt->rowCount==1){
echo "<script>alert('blah.. blah.. blah.')</script>";
}else{ 
echo "<script>alert('blah.. blah.. blah.')</script>";
}//EndIf
}//EndFunction
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-03-30 02:58:17

这一行代码是你的问题,有适当的用法。

语法

代码语言:javascript
复制
$var = "{array['key']}";

请注意:

  • “更大的”字符串使用"“(因为”字符串“允许对变量使用{},而”内部“则允许使用'‘)。
  • {}告诉php用变量的值替换
  • 数组的成员是要使用的变量

您的错误是使用多行,对于多行您可能会这样做(在许多其他语法中,请研究它们):

代码语言:javascript
复制
$var = "this " .
           "is " .
             " a string in multiple lines";

2)您的sql字符串语法是错误的,描述很可能是字符串类型,字符串必须介于“”之间

代码语言:javascript
复制
$sql = "INSERT INTO (...) VALUES ( 'my description' )"; //you lack these 's, put them
票数 2
EN

Stack Overflow用户

发布于 2021-02-13 04:44:45

若要传递空关联数组,请执行以下操作:

$result['data'] = array('content_id'=>'','user_id'=>'','title' =>'','content' =>'');

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29337654

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档